Back
Next
XSLT variables actually have a lot more in common with constants in many programming languages
such as XSLT and are
used for a similar purpose in each way. If we use the same value multiple times
in our style sheet, and there's a
possibility that we'll have to change them all to a different value, it's better to assign that value
to a variable and use references to the variable instead. Then, if we need to change the
value when re-using the style sheet, we only change the value assigned in the creation of that variable
in XSLT.
XSLT variables are more common like a variable in algebra than with a variable in a
typical programming language. It's a name that represents a value and, within a
specific
application of a template, it will never represent any other value -- it can't be reset using
anything described in the XSLT Recommendation.
Some XSLT processors offer a special extension function to allow the resetting
of variables.
The <xsl:variable> element declares a global or local variable in a style sheet and gives it
a value. Because XSLT permits no side-effects, once the value of
the variable has been well-known, it remains the same until the variable goes out of scope.
Here in XSLT Tutorial you will learn all about to use of XSLT Variable.
Syntax for XSLT Variables
<xsl:variable name=NAME select=EXPRESSION >
TEMPLATE
</xsl:variable>
Example of XSLT Variables
XML Code for XSLT Variable Example [xslt_variable_example.xml]
<?xml version="1.0" encoding="ISO-8859-15"?>
<?xml-stylesheet type="text/xsl" href="xslt_variable_example.xsl"?>
<globalGuideLine>
<employee>
<FirstName>Austin</FirstName>
<LastName>Hennery</LastName>
<gender>m</gender>
</employee>
<employee>
<FirstName>Angela</FirstName>
<LastName>Julie</LastName>
<gender>f</gender>
</employee>
......
......
......
</globalGuideLine>
XSLT Source file for XSLT Varialbe Example [xslt_variable_example.xsl]
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
xmlns="https://www.w3.org/1999/xhtml">
<body style="font-family:Arial,helvetica,sans-serif;font-size:12pt;
background-color:#EEEEEE">
<xsl:variable name="xslt_variable_size">10pt</xsl:variable>
<xsl:variable name="xslt_variable_color">blue</xsl:variable>
<xsl:for-each select="globalGuideLine/employee">
<div style="background-color: #cccccc;color:000000;padding:0px">
<span style="color:#000000">
<font size="{$xslt_variable_size}" color="{$xslt_variable_color}">
<xsl:value-of select="FirstName"/> <xsl:value-of select="LastName"/>
</font>
</span>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<xsl:value-of select="description"/>
<span style="font-style:none">
<xsl:if test="gender='m'">
<font color="{$xslt_variable_color}">
<b>Mr. </b>
</font>
<xsl:value-of select="FirstName"/>
is working at Global Guide Line
</xsl:if>
<xsl:if test="gender='f'">
<font color="{$xslt_variable_color}"><b>Miss </b></font>
<xsl:value-of select="FirstName"/> is working at Global Guide Line
</xsl:if>
</span>
</div>
</xsl:for-each>
</body>
</html>
Sources of XSLT Variables Examples
XSLT Tutorial section of complete sources and outputs view by simply clicking on file name of XSLT, XML,
Out Put XSLT Variable files below.
Back
Next
|