XSL reference documentation generated from the W3C Recommendation 16 November 1999
<variable>
name = qname
select = expression
Model: template
</variable>
<param>
name = qname
select = expression
Model: template
</param>
A variable is a name that may be bound to a value. The value to
which a variable is bound (the xsl:variable and xsl:param. The difference
is that the value specified on the xsl:param variable is
only a default value for the binding; when the template or stylesheet
within which the xsl:param element occurs is invoked,
parameters may be passed that are used in place of the default
values.
Both xsl:variable and xsl:param have a
required name attribute, which specifies the name of the
variable. The value of the name attribute is a QName, which is expanded as described
in qname.
For any use of these variable-binding elements, there is a region of the stylesheet tree within which the binding is visible; within this region, any binding of the variable that was visible on the variable-binding element itself is hidden. Thus, only the innermost binding of a variable is visible. The set of variable bindings in scope for an expression consists of those bindings that are visible at the point in the stylesheet where the expression occurs.
Variables introduce an additional data-type into the expression
language. /, //, and [] operators on
result tree fragments. When a permitted operation is performed on a
result tree fragment, it is performed exactly as it would be on the
equivalent node-set.
When a result tree fragment is copied into the result tree (see copy-of), then all the nodes that are children of the root node in the equivalent node-set are added in sequence to the result tree.
Expressions can only return values of type result tree fragment by referencing variables of type result tree fragment or calling extension functions that return a result tree fragment or getting a system property whose value is a result tree fragment.
A variable-binding element can specify the value of the variable in three alternative ways.
If the variable-binding element has a select
attribute, then the value of the attribute must be an
If the variable-binding element does not have a select
attribute and has non-empty content (i.e. the variable-binding element
has one or more child nodes), then the content of the
variable-binding element specifies the value. The content of the
variable-binding element is a template, which 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 URI
of the variable-binding element.
It is an error if a member of the sequence of nodes created by instantiating the template is an attribute node or a namespace node, since a root node cannot have an attribute node or a namespace node as a child. An XSLT processor may signal the error; if it does not signal the error, it must recover by not adding the attribute node or namespace node.
If the variable-binding element has empty content and does not have
a select attribute, then the value of the variable is an
empty string. Thus
<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:
<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[position()=$n]"/>
Note: One convenient way to specify the empty node-set as the default value of a parameter is:
<xsl:param name="x" select="/.."/>
xsl:copy-of
<copy-of>
select = expression
Model: EMPTY
</copy-of>
The xsl:copy-of element can be used to insert a result
tree fragment into the result tree, without first converting it to a
string as xsl:value-of does (see value-of). The required select attribute
contains an xsl:value-of.
Both xsl:variable and xsl:param are
allowed as xsl:param element
declares a parameter to the stylesheet; XSLT does not define the
mechanism by which parameters are passed to the stylesheet. It is an
error if a stylesheet contains more than one binding of a top-level
variable with the same name and same
This example declares a global variable para-font-size,
which it references in an attribute value template.
<xsl:variable name="para-font-size">12pt</xsl:variable>
<xsl:template match="para">
<fo:block font-size="{$para-font-size}">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
As well as being allowed at the top-level, both
xsl:variable and xsl:param are also
allowed in templates. xsl:variable is allowed anywhere
within a template that an instruction is allowed. In this case, the
binding is visible for all following siblings and their descendants.
Note that the binding is not visible for the xsl:variable
element itself. xsl:param is allowed as a child
at the beginning of an xsl:template element. In this
context, the binding is visible for all following siblings and their
descendants. Note that the binding is not visible for the
xsl:param element itself.
xsl:variable or xsl:param element within a
template xsl:variable or xsl:param
element also within the template. It is not an error if a binding
established by an xsl:variable or xsl:param
element in a template xsl:variable or
xsl:param
<xsl:template name="foo"> <xsl:param name="x" select="1"/> <xsl:variable name="x" select="2"/> </xsl:template>
However, the following is allowed:
<xsl:param name="x" select="1"/> <xsl:template name="foo"> <xsl:variable name="x" select="2"/> </xsl:template>
Note: The nearest equivalent in Java to an xsl:variable
element in a template is a final local variable declaration with an
initializer. For example,
<xsl:variable name="x" select="'value'"/>has similar semantics to
final Object x = "value";XSLT does not provide an equivalent to the Java assignment operator
x = "value";because this would make it harder to create an implementation that processes a document other than in a batch-like way, starting at the beginning and continuing through to the end.
<with-param>
name = qname
select = expression
Model: template
</with-param>
Parameters are passed to templates using the
xsl:with-param element. The required name
attribute specifies the name of the parameter (the variable the value
of whose binding is to be replaced). The value of the
name attribute is a QName, which is expanded as described
in qname. xsl:with-param is allowed
within both xsl:call-template and
xsl:apply-templates. The value of the parameter is
specified in the same way as for xsl:variable and
xsl:param. The current node and current node list used
for computing the value specified by xsl:with-param
element is the same as that used for the
xsl:apply-templates or xsl:call-template
element within which it occurs. It is not an error to pass a
parameter x to a template that does not have an
xsl:param element for x; the parameter is
simply ignored.
This example defines a named template for a
numbered-block with an argument to control the format of
the number.
<xsl:template name="numbered-block">
<xsl:param name="format">1. </xsl:param>
<fo:block>
<xsl:number format="{$format}"/>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="ol//ol/li">
<xsl:call-template name="numbered-block">
<xsl:with-param name="format">a. </xsl:with-param>
</xsl:call-template>
</xsl:template>