Name
<xsl:variable>
Defines a variable. If <xsl:variable> occurs as a top-level
element, it is a global variable that is accessible throughout the
stylesheet. Otherwise, the variable is local and exists only in the
element that contains the <xsl:variable>. The value of the
variable can be defined in one of two ways: specified in the select attribute or defined in an XSLT
template inside the <xsl:variable> element itself. If
neither method is used, the value of the variable is an empty
string.
Category
Either a top-level element or an instruction.
Required Attribute
nameAn attribute that names this variable.
Optional Attributes
selectAn XPath expression that defines the value of this variable.
- [2.0 – Schema]
as The datatype of the variable. For example,
<xsl:variablename="age"as="xs:integer" select="11"/>defines a new variable of typexs:integer. It is an error if the supplied value can’t be converted to the specified type; using the attributeselect="'really, really old'"here would cause an error. If you’re using a schema-aware XSLT processor, you can use your own datatypes.
Content
The <xsl:variable>
element can be empty or it can contain an XSLT template. It is a fatal
error if it contains both content and a select attribute.
Appears in
<xsl:stylesheet> as a
top-level element or in a template.
Defined in
[1.0] XSLT section 11, “Variables and Parameters.”
[2.0] XSLT section 9, “Variables and Parameters.”
Example
Here is a stylesheet that defines a number of variables:
<?xml version="1.0"?>
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access