How do I create Variables in XSLT that are not document fragments?
Posted
by chiborg
on Stack Overflow
See other posts from Stack Overflow
or by chiborg
Published on 2010-06-08T11:49:15Z
Indexed on
2010/06/08
11:52 UTC
Read the original article
Hit count: 258
Consider the following XSLT template
<xsl:template match="/">
<xsl:variable name="var1">
<elem>1</elem>
<elem>2</elem>
<elem>3</elem>
</xsl:variable>
<xsl:text>var1 has </xsl:text>
<xsl:value-of select="count($var1)"/>
<xsl:text>elements.
</xsl:text>
<xsl:variable name="var2" select="$var1/elem[. != '2']"/>
<xsl:text>var2 has </xsl:text>
<xsl:value-of select="count($var2)"/>
<xsl:text>elements.
</xsl:text>
</xsl:template>
The output of this template is
var1 has 1 elements
var2 has 2 elements
The first line outputs 1 (and not, as I first expected 3) because var1
is a document fragment that contains the <elem>
elements as childen. Now for my questions:
How can I create a variable that does not contain a document fragment? I could do it like I did with var2
, only leaving out the predicate. But maybe there is a way without using a second variable. Or, as an alternative: How can I preserve the document fragment in a variable while filtering out some elements?
© Stack Overflow or respective owner