Re: [xsl] Need to output an opening or closing tag by itself

Subject: Re: [xsl] Need to output an opening or closing tag by itself
From: "Luke Stedman" <luke.stedman@xxxxxxxxx>
Date: Wed, 25 Jun 2008 08:22:58 +0100
Not pretty, but this will work....

   <xsl:choose>
     <xsl:when test="@t ='a'">
       <xsl:text disable-output-escaping="yes">&lt;e&gt;</xsl:text>
     </xsl:when>
     <xsl:when test="@t ='b'">
       <xsl:text disable-output-escaping="yes">&lt;/e&gt;</xsl:text>
     </xsl:when>
     <xsl:otherwise>
       <xsl:message terminate="yes">
         Unexpected data in attribute t.
       </xsl:message>
     </xsl:otherwise>
   </xsl:choose>

...this is a slightly better solution...

 <xsl:template match="/root">
   <root>
     <xsl:apply-templates select="e"/>
   </root>
 </xsl:template>

 <xsl:template match="e">
     <xsl:if test="@t ='a' and ./following-sibling::e[1]/@t = 'b'">
		<e>
			<xsl:for-each select="./following-sibling::f">
				<xsl:apply-templates select="."/>
			</xsl:for-each>
		</e>
     </xsl:if >
 </xsl:template>

 <xsl:template match="f">
   <xsl:copy/>
 </xsl:template>

Cheers
Luke

2008/6/25 David Frey <dpfrey@xxxxxxx>:
> I have a simple XSLT problem for the gurus.
>
>
> This is my input document:
> <root>
>  <e t="a"/>
>  <f/>
>  <f/>
>  <e t="b"/>
> </root>
>
>
> This is the output document I want to produce:
> <root>
>  <e>
>    <f/>
>    <f/>
>  </e>
> </root>
>
>
> This is the stylesheet I wrote to do the transformation:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet
>  version="1.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>  <xsl:template match="/root">
>    <root>
>      <xsl:apply-templates/>
>    </root>
>  </xsl:template>
>
>  <xsl:template match="e">
>    <xsl:choose>
>      <xsl:when test="@t ='a'">
>        <e>
>      </xsl:when>
>      <xsl:when test="@t ='b'">
>        </e>
>      </xsl:when>
>      <xsl:otherwise>
>        <xsl:message terminate="yes">
>          Unexpected data in attribute t.
>        </xsl:message>
>      </xsl:otherwise>
>    </xsl:choose>
>  </xsl:template>
>
>  <xsl:template match="f">
>    <xsl:copy/>
>  </xsl:template>
> </xsl:stylesheet>
>
>
> Here is the Saxon output:
> $ java -jar "c:\Program Files\Saxon\saxon9.jar" -o out.xml in.xml
> test.xslt
> Error on line 16 column 9 of
> file:/C:/Documents%20and%20Settings/DFREY/Desktop/test/test.xslt:
>  SXXP0003: Error reported by XML parser: The element type "e" must be
> terminated by the
>  matching end-tag "</e>".
> Failed to compile stylesheet. 1 error detected.
>
>
> I understand that the source of the error is that the XSLT file is not
> valid XML because the <e> tag doesn't have a matching </e> tag in the
> right place.
>
> How can I work around this issue to solve my problem?
>
> Thanks,
> David

Current Thread