XSL Basics (part 2) - Flavour Of The Month (
Page 8 of 9 )
Just as XML has entities, pre-defined constants which
can be used at different places within your document, so too does XSLT have variables.
However, XSLT variables are not the same as the ones you may be used to working
with in standard programming languages. XSLT variables, once defined, cannot be
altered; they remain static and can merely be used at different places within
the XSLT stylesheet.
XSLT variables are defined with the <xsl:variable> instruction, and a value
is attached to them either via a "select" attribute or enclosed within the opening
and closing tags of the instruction; this value may be a literal, or obtained
from an expression. In case a variable lacks both a "select" attribute and a content
value, it is automatically assigned an empty string value.
Variables may be defined either within or outside template rules. A variable
defined outside a template rule (at the top-level of the document) is globally
available, while a variable defined within a template rule is available only to
descendants of that rule.
Once defined, variables can be accessed by referencing the variable name with
a preceding dollar($) symbol.
Consider the following stylesheet, again with reference to the preceding XML
data.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable
name="flavour">strawberry</xsl:variable>
<xsl:template match="/">
<html>
<head>
<body>
--
root level -- My favourite flavour is <xsl:value-of select="$flavour"
/><br
/>
<xsl:apply-templates select="recipe/ingredients" />
<xsl:apply-templates
select="recipe/process" />
-- root level -- My favourite flavour is <xsl:value-of
select="$flavour"
/><br />
</body>
</head>
</html>
</xsl:template>
<xsl:template
match="ingredients/item">
<xsl:variable name="flavour">raspberry</xsl:variable>
--
item level -- My favourite flavour is <xsl:value-of select="$flavour"
/><br
/>
</xsl:template>
<xsl:template match="process/step">
-- step level --
My favourite flavour is <xsl:value-of select="$flavour"
/><br />
</xsl:template>
</xsl:stylesheet>
Here, I've first set up a global variable, $flavour, with the value "strawberry".
I've displayed it in my very first template rule, which matches the document root.
Since this template rule does not contain any overriding variable definition,
it will use and print the value of the global variable, "strawberry".
Then I've transferred control to the second template rule, which matches "item"
elements. Over here, I've defined a new value for the variable, "raspberry". This
time, the value of the global variable will be superseded by the value within
the template rule, and an attempt to print $flavour will return "raspberry".
Once that node collection is exhausted, control goes to the third template rule,
which matches "step" elements. This rule does not contain any variable definition;
further, it does not have access to the value defined within the second template
rule, since it is not a direct descendant of that rule. Consequently, it will
also use the value "strawberry".
Here's the output:
-- root level -- My favourite flavour is strawberry
-- item level -- My favourite
flavour is raspberry
-- item level -- My favourite flavour is raspberry
-- item
level -- My favourite flavour is raspberry
-- step level -- My favourite flavour
is strawberry
-- step level -- My favourite flavour is strawberry
-- step level
-- My favourite flavour is strawberry
-- step level -- My favourite flavour is
strawberry
-- step level -- My favourite flavour is strawberry
-- step level --
My favourite flavour is strawberry
-- root level -- My favourite flavour is strawberry