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

name

An attribute that names this variable.

Optional Attributes

select

An XPath expression that defines the value of this variable.

[2.0 – Schema] as

The datatype of the variable. For example, <xsl:variable name="age" as="xs:integer" select="11"/> defines a new variable of type xs:integer. It is an error if the supplied value can’t be converted to the specified type; using the attribute select="'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"?>

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.