Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
Specifies a value bound in an expression.
Syntax
<xsl:variable name = QName select = expression> </xsl:variable> |
Attributes
- name
-
[required] Specifies the name of the variable.
- select
-
The value of the attribute is an expression, and the value of the variable is the object that results from evaluating the expression. When this attribute is specified, the content of <xsl:variable> must be empty.
Element Information
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 Element, xsl:template, xsl:when, xsl:with-param, output elements |
Child elements |
xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:text, xsl:value-of, output elements |
Remarks
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:
- If the element has a
selectattribute, the value of the attribute must be an
expression and the value of the variable is the object that results
from evaluating the expression. In this case, the content of the
element must be empty.
- If the element does not have a
selectattribute and has non-empty content such as one or
more child nodes, the content specifies the value. The content is a
template that is instantiated to give the value of the variable.
The value is a result-tree fragment equivalent to a node-set
containing just a single root node having as children the sequence
of nodes produced by instantiating the template. The base URI of
the nodes in the result-tree fragment is the base Uniform Resource
Identifier (URI) of the variable-binding element.
An error occurs if a member of the sequence of nodes created by instantiating the template is an attribute node or a name space node, because a root node cannot have an attribute node or a name space node as a child. - If the content is empty and does not have a
selectattribute, the value of the variable is an empty
string. Thus
Copy Code <xsl:variable name="x"/>
Copy Code <xsl:variable name="x" select="''"/>
Note: When a variable is used to select nodes by position, be careful not to do the following. Copy Code <xsl:variable name="n">2</xsl:variable> ... <xsl:value-of select="item[$n]"/>
Copy Code <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: The following is a convenient way to specify the empty node-set as the default value of a parameter. Copy Code <xsl:param name="x" select="/.."/>