xsl:param
xsl:choose
xsl:for-each
xsl:import
xsl:with-param
ELEMENT: xsl:variable
<xsl:variable
name="qname"
>
</xsl:variable>
Or:
<xsl:variable
name="qname"
select="expression"
/>
The
xsl:variable
element is used to declare a local or global variable and to give that variable a name and a value. The value can be assigned by either the content of the
xsl:variable
element or by the
select
attribute, but not by both. Each variable declaration requires a separate
xsl:variable
element. Global variables are declared in the top level of the style sheet (as children of the
xsl:stylesheet
or
xsl:transform
elements). Local variables are declared within the template body.
Note that once you set a variable's value, there is no provision in the XSLT language to change or modify that value. This is in sharp contrast to most other computer languages which allow you to repeatedly reassign variable values.
The
xsl:param
element can also be used to declare parameters. The only real difference between a variable and a parameter is how the value is assigned.
Like all XSLT elements, the
xsl:variable
element must be closed (well-formed). If the
select
attribute is present, then this element is self-closing. If the
select
attribute is not present, then this element is not self-closing and the separate closing element is mandatory.
name="qname"
The mandatory
name
attribute is the qname of the expression. A qname is a qualified name that is composed of an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). Note the following rules concerning when two different variables can have the same name. (The same rules apply to the
name
attribute of the
xsl:param
element.)
A name can be repeated if one of the names is in an imported stylesheet and therefore has a lower import precedence. Under these circumstances, the higher import precedence name will always have precedence.
Two different variables can have the same name if they can never occur within the same scope. Therefore no ambiguity can occur (which would be an error).
A local and global variable can have the same name. However, when the local variable is in scope, the global variable cannot be accessed.
If the
variable
element contains no content and a
select
attribute has been not assigned, then the named variable is set to be the empty string.
select="expression"
The optional
select
attribute is an expression that defines the variable. If the
select
attribute is present, then the
xsl:variable
element cannot contain any content and is self-closing. If an expression is given, the data type must be Boolean, node-set, number or string. If it is a literal string, the string must be enclosed within opening and closing quotes and in turn, that value must be enclosed again in opening and closing quotes. For example:
<xsl:variable name="car" select=" ' Ford ' " />
Or
<xsl:variable name="car" select=' " Ford " ' />
If the
name
attribute is assigned, a
select
attribute is not assigned, and there is no content, then the named variable is set to be the empty string.
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_variable.xsl"?>
and we name it: xslt_example_variable.xml
We create a reuseable table header.
Code for xslt_example_variable.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<
xsl:variable name="staff_table"
>
<tr>
<td><b>Name</b></td>
<td><b>Age</b></td>
</tr>
<
/xsl:variable
>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age < 35">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<p />
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age > 34">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Click to view output in separate window
- requires Internet Explorer
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information