Specifies a value bound in an expression.
<xsl:variable name = Qname select = expression> </xsl:variable>
Number of occurrences | Unlimited |
Parent elements | xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:stylesheet xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements |
Child elements | xsl:apply-templates, xsl:attribute, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements |
The value of the variable may be an object of any type that can be returned by an expression. The <xsl:variable> element can specify the value of the variable in three alternative ways:
An error occurs if a member of the sequence of nodes created by instantiating the template is an attribute node or a namespace node, because a root node cannot have an attribute node or a namespace node as a child.
<xsl:variable name="x"/>
is equivalent to
<xsl:variable name="x" select="''"/>
Note When a variable is used to select nodes by position, be careful not to do the following:
<xsl:variable name="n">2</xsl:variable> ... <xsl:value-of select="item[$n]"/>
This will output the value of the first item element, because the variable "n" will be bound to a result tree fragment, not a number. Instead, do either
<xsl:variable name="n" select="2"/> ... <xsl:value-of select="item[$n]"/>
or
<xsl:variable name="n">2</xsl:variable> ... <xsl:value-of select="item[number($n)]"/>
Note One convenient way to specify the empty node-set as the default value of a parameter is:
<xsl:param name="x" select="/.."/>