Variables
If we use logic to control the flow of our stylesheets, we’ll probably want to store temporary results along the way. In other words, we’ll need to use variables. XSLT provides the <xsl:variable> element, which allows you to store a value and associate it with a name.
The <xsl:variable> element can be used in three ways. The simplest form of the element creates a new variable whose value is an empty string (""). Here’s how it looks:
<xsl:variable name="x"/>
This element creates a new variable named x, whose value is an empty string. (Please hold your applause until the end of the section.)
You can also create a variable by adding a select attribute to the <xsl:variable> element:
<xsl:variable name="favouriteColour" select="'blue'"/>
In this case, we’ve set the value of the variable to be the string “blue”. Notice that we put single quotes around the value. These quotes ensure that the literal value blue is used as the value of the variable. If we had left out the single quotes, this would mean the value of the variable is that of all the <blue> elements in the current context, which definitely isn’t what we want here.
Warning
Some XSLT processors don’t require you to put single quotes around a literal value if the literal value begins with a number. This is because the XML specification states that XML element names can’t begin with a number. If I say the value should be 35, Xalan, XT, and Saxon all assume that I mean 35 as a literal value, not as an element name. Although ...