xsl:variable
Binds a variable in the current scope.
<xsl:variable name="count" select="count(item)"/>A variable can take its value from a select expression or from a sequence constructor body that produces a fragment. Variables are immutable — once bound, the value cannot be changed. Their scope is the rest of the containing element. At the stylesheet (top) level, variables are global.
When as is given, the value is type-checked and (where possible) coerced to the declared type.
Attributes
| Attribute | Description |
|---|---|
name |
Variable name (without $). Required. |
select |
XPath expression for the value. If absent, the body provides the value. |
as |
Sequence type the value is coerced to. |
Examples
XPath value:
<xsl:variable name="total" select="sum(item/@price)"/>Body value (produces a document fragment):
<xsl:variable name="header">
<h1>Title</h1>
</xsl:variable>
<xsl:copy-of select="$header"/>Typed sequence:
<xsl:variable name="primes" as="xs:integer*" select="2, 3, 5, 7, 11"/>Element-typed body:
<xsl:variable name="rows" as="element(row)*">
<row>a</row>
<row>b</row>
<row>c</row>
</xsl:variable>