Skip to Main Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced content levelIntermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano
XSLT and XPATH: A Guide to XML Transformations

XSLT and XPATH: A Guide to XML Transformations

John Robert Gardner, Zarella L. Rendon

Publisher Resources

ISBN: 0596000537Supplemental ContentCatalog PageErrata