XSLT variables can be either be global or local in scope. If the variable is declared as a top-level element to the stylesheet then the variable’s scope is global. Otherwise the variable is within the local scope of the declaring template.
XSLT variables are more like named constants: They cannot be changed once they are set.
XSLT variables can be referenced using the form “$name” from a relevant XSL declaration.
|name||REQUIRED. Specifies the variable name. The name must be unique within the declaring scope.|
|select||OPTIONAL. Specifies an XPATH expression that defines the variable content. The variable content type is determined by the return-type of the expression. This includes literal expressions as well.|
If the variable is defined with only a name; and no select or content, then the variable defaults to an empty string. This example is the no-value equivalent of the content template value example.
The select attribute contains any XPATH expression that results in a value.
Literal values can be defined as a string values enclosed in quotes. This is otherwise known as a constant.
XPATH expressions NOT enclosed in quotes are evaluated against the source XML document:
Content Template Values
xsl:variables can define the variable content as either static or generated values. The example shows an example where both are combined: One book is hardcoded, the second books’ title is built from XPATH expressions.
To select, work with or otherwise manipulate a variable, select the variable by its name
… select =”$name” …
Template Return Values
A useful technique is to set the variable equal to the results of another template. This allows you to reuse the results of the template without the need to invoke it again.
Node Variable Values
A node can also be defined as a variable value.
Define a constant by specifying a literal value as the variables’ value.