Declaring and Using Variables

A variable in XSLT has more in common with a variable in algebra than with a variable in a typical programming language. It’s a name that represents a value and, within a particular application of a template, it will never represent any other value — it can’t be reset using anything described in the XSLTRecommendation.
(Some XSLT processors offer a special extension function to allow the resetting of variables.)
XSLT variables actually have a lot more in common with constants in many programming languages and are used for a similar purpose. If you use the same value multiple times in your stylesheet, and there’s a possibility that you’ll have to change them all to a different value, it’s better to assign that value to a variable and use references to the variable instead.
Then, if you need to change the value when re-using the stylesheet, you only change the value assigned in the creation of that variable.
The xsl:variable element is used to declare a local or global variable and to give that variable a name and a value. The value can be assigned by either the content of the xsl:variable element or by the select attribute, but not by both.
Each variable declaration requires a separate xsl:variable element. Global variables are declared in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local variables are declared within the template body.
Note that once you set a variable’s value, there is no provision in the XSLT language to change or modify that value. This is in sharp contrast to most other computer languages which allow you to repeatedly reassign variable values.
The xsl:param element can also be used to declare parameters. The only real difference between a variable and a parameter is how the value is assigned.
Like all XSLT elements, the xsl:variable element must be closed (well-formed). If the select attribute is present, then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the separate closing element is mandatory.
Declaring Variables
<xsl:variable> allows you to associate a variable with a string, node list, or result tree fragment.
• Variables are “single assignment” (no side effects)

• Variables are lexically scoped

• Variables assigned inside a conditional only apply inside that conditional.

This is rarely correct:
<xsl:if test=”$foo”>

<xsl:variable name=”bar”>…</xsl:variable> </xsl:if>

Usually, you want to put the conditional inside the variable
<xsl:variable name=”bar”>
<xsl:if test=”$foo”>

</xsl:if>
</xsl:variable>
Using Variables
After variables (or parameters) have been declared, they can be used in two places:
• In XSL element attributes that expect an expression. These are summarized in the following table:

XSLT Element

Attribute

xsl:apply-templates select
xsl:value-of select
xsl:number value
xsl:for-each select
xsl:if test
xsl:when test
xsl:sort select
• In attribute value templates; attribute value templates have the form “{$variable}”. They are allowed in the following places:

Element

Attribute

Literal result elements any attribute
xsl:element name
namespace
xsl:attribute name
namespace
xsl:number level
count
from
format
lang
grouping-separator
grouping-size
xsl:sort order

lang

data-type

case-order

xsl:processing-instruction name
• To insert a literal “{” character in a context where attribute value templates are expanded, use “{{“.
Source code for xsl_variable_example.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE xsl:stylesheet[

	<!ENTITY nbsp " ">

 ]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="iso-8859-1" />

<!-- XSL Variable Declaration-->

<xsl:variable name="tbl">

 	<tr>

		<td><b>Name</b></td>

		<td><b>Address</b></td>

		<td><b>Department</b></td>

		<td><b>Designation</b></td>

		<td><b>Age</b></td>

		

		

	</tr>

 </xsl:variable>

 <xsl:template match="/">



<ebizml>

<head>

<title>XSLT xsl:choose Example</title>

</head>

<body>



<span style="width 600px;border: 2px outset blue solidl;display:block">





</span><br /> 

<table>

<xsl:copy-of select="$tbl" />

</table>

</body>

</ebizml>



</xsl:template>

</xsl:stylesheet>