home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeXSLSearch this book

4.5. 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 this works with many XSLT processors, you're safer to put the single quotes around the numeric values anyway. A further aside: the value here is the string "35", although it can be converted to a number easily.

The third way to use the <xsl:variable> element is to put content inside it. Here's a brief example:

<xsl:variable name="y">
    <xsl:when test="$x &gt; 7">

In this more complicated example, the content of the variable y depends on the test attribute of the <xsl:when> element. This is the equivalent of this procedural programming construct:

int y;
if (x > 7)
  y = 13;
  y = 15;

4.5.1. Are These Things Really Variables?

Although these XSLT variables are called variables, they're not variables in the traditional sense of procedural programming languages like C++ or Java. Remember that earlier we said one goal behind the design of the stylesheet language is to avoid side effects in execution? Well, one of the most common side effects used in most procedural languages is changing the value of a variable. If we write our stylesheet so that the results depend on the varying values of different variables, the stylesheet engine would be forced to evaluate the templates in a certain order.

XSLT variables are more like variables in the traditional mathematical sense. In mathematics, we can define a function called square(x) that returns the value of a number (represented by x) multiplied by itself. In other words, square(2.5) returns 6.25. In this context, we understand that x can be any number; we also understand that the square function can't change the value of x.

It takes a while to get used to this concept, but you'll get there. Trust me on this.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.