Setting Variables

 
xslt for dummies
Chapter 8 - Variables in XSLT: A Breed Apart
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

A variable is defined in XSLT in one of two ways: either as a content variable or a select attribute variable .

Content variables

The most straightforward way to set a variable is by using the xsl:variable element to assign a variable name to the content enclosed by its tags.

 <xsl:variable name="mystate">Massachusetts</xsl:variable> 

In the preceding example, the mystate variable represents the string value of Massachusetts . Variables come in handy when you dont want to type out long pieces of text over and over again in your stylesheet.

In order to use a content variable, you first need to let the XSLT processor know your intentions. You cant just enter the variables name inside the stylesheet as is and expect it to be treated properly. XSLT processors are smart, but they dont know whether you want it to be handled as normal text or as a variable identifier. If you dont tell the processor that a variable is on its way, it opts for the easy road and treats it as ordinary text. Thus, to tell the processor that you want to use mystate as a variable, prefix it with a $ character.

 $mystate 

This fully prepared variable then needs to be plugged into your stylesheet in the appropriate context. Just exactly where you place it in your stylesheet depends on how you want the variable to be placed in the result document.

If you want to output the variable as text, use the xsl:value-of element,

 <p>I love <xsl:value-of select="$mystate"/> in the Fall.</p> 

which is spit out in HTML as

 <p>I love Massachusetts in the Fall.</p> 

(See Chapter 4 for more information on using the xsl:value-of instruction.)

Or, to insert the variable into your XML output as an attribute, use an attribute value template,

 <location state="{$mystate}"/> 

which results in

 <location state="Massachusetts"/> 

(See Chapter 5 for more information on attribute value templates.)

The value of a content variable is always the piece of text enclosed by the xsl:variable element. Even a number placed inside is converted to a string during transformation.

If youve been around computers much, youve probably heard the term WYSIWYG (What You See Is What You Get)what you see on-screen looks the same as when its printed. Content variables could similarly be described as WYSIWYG because what you see in the XSLT markup that you create is exactly what appears in the result document.

Select attribute variables

A second way to define a variable is to use the xsl:variable element with a select attribute added to it.

 <xsl:variable name="mystate" select="'Massachusetts'"/> 

This kind of variable is sometimes referred to as a select attribute variable. Whereas a content variable sees all values as text, a select attribute variable treats its value as the result of an expression. An expression is an XSLT statement that represents a value or is used to calculate a value. See Table 8-1 for some sample XSLT expressions and their results. Content variables are WYSIWYG, but select attribute variables often are not. As you can see in Table 8-1, the result of an expression typically looks different than the expression itself.

Table 8-1: Results of XSLT Expressions

Expression

Result

8-5

3

˜Text

Text

round(5.129)

5

In the code sample at the beginning of this section, notice that I add single quotation marks around Massachusetts when placing it inside of a select attribute. If I dont add single quotation marks, XSLT tries to evaluate Massachusetts as an expression. Admittedly, Ive certainly heard my share of expressions in Massachusettsparticularly from other drivers when Im driving around downtown Bostonbut thats not what I had in mind here. If I dont add the quotes, I get a goose egg from the XSLT processor when I try to plug in that variable into the stylesheet. But by including the quotes, I let the processor know that it should treat Massachusetts as a literal string.

Although you can use a select attribute variable anywhere that you would use a content variable for outputting text, its primary use is working with number values or node sets.

Remember these three final gotchas when you define variables:

  • If you have a select attribute defined for an xsl:variable , you cant define any content (text between the start and end tags) or you get a processing error.

  • Conversely, if the variable doesnt have any content defined and doesnt have a select attribute, its result is an empty string.

  • A content variable needs to have a start and an end tag (<xslt: variable name="x">y</xsl:variable>) , whereas a select attribute variable should use an empty tag (<xslt:variable name="x" select="'y'"/>) because it contains no content.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net