Think Globally, Act Locally

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

Youve undoubtedly seen the slogan Think globally, act locally on bumper stickers. My personal favorite bumper sticker is Visualize Whirled Peas , but flying veggies are quite distracting when you try to work with variables, so Ill opt for the more environmentally sound expression.

Although the Think globally, act locally slogan is appropriate for conserving nature, its also an ideal maxim for using variables because all variables have scope. A variables s cope is the area of the stylesheet in which the variable is available for use. Variables can be available to the entire document (globally) or simply to a specific region (locally).

Throughout previous chapters of this book, I define variables at the beginning of the XSLT stylesheets and then use them in templates defined at the same tree level. When you declare a variable at the top level of a document, however, you define a global variable, meaning that you can use this variable anywhere in the stylesheet and that its always available.

You can also define variables inside of a given template rule. When you do so, this local variable is available to only that template rule or any elements that are inside of it. For example, the variable thinkGlobally can be used anywhere in this stylesheet:

 <xsl:variable name="thinkGlobally">14pt</xsl:variable> <xsl:template match="film"> <font size="{$thinkGlobally}"> <xsl:apply-templates select="writer"/> </font><xsl:text> </xsl:text> <xsl:variable name="actLocally">12pt</xsl:variable> <font size="{$actLocally}"> <xsl:apply-templates select="director"/> </font> </xsl:template> 

to output the following HTML:

 <font size="14">Kenneth Branagh, William Shakespeare</font> <font size="12">Kenneth Branagh</font> 

Although globals can be used anywhere, the actLocally variable can be used only inside the template rule in which it is defined. If you try to use it outside of the template rule it is in, the XSLT processor yells at you.

A second issue to consider when scoping variables is precedence when two variables have identical names . In other words, who wins the duel? In XSLT, the variable farthest down the hierarchy always wins; a local variable always overrides its global counterpart . So if you tweak the example and change the name of the local variable to also be thinkGlobally , the XSLT processor gives the writer element a value of 14pt and the director element a value of 12pt :

 <xsl:template match="film"> <font size="{$thinkGlobally}"> <xsl:apply-templates select="writer"/> </font><xsl:text> </xsl:text> <xsl:variable name="thinkGlobally">12pt</xsl:variable> <font size="{$thinkGlobally}"> <xsl:apply-templates select="director"/> </font> </xsl:template> 

with identical output as before:

 <font size="14">Kenneth Branagh, William Shakespeare</font> <font size="12">Kenneth Branagh</font> 

Through these examples, you can see that Think globally, act locally holds true. Although you need to be aware of global variables that affect the stylesheet as a whole, in the end, the value of the variable being used depends wholly on the local conditions in the template rule in which its being used.

2000-2002    Feedback

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: