XML Tutorials - Herong's Tutorial Examples - Version 5.05, by Dr. Herong Yang
"variable" - Declaring Variables
This section describes the 'variable' element, which is used in the content of a 'template' element. The 'variable' element is an XSLT statement that declares a variable and assigns a value to the variable.
"variable": A child element used as part of the content of a "template" element. A "variable" element serves as a variable declaration and assignment statement. The syntax of the "variable" element is:
<xsl:variable name="variable_name"> variable_value </xsl:variable>
The variable name is the value of the "name" attribute. The variable value is the content of the statement element.
Once a variable is defined, its value can be referred by its name pre-fixed with a dollar sign "$". A variable can only be accessed in the same sub-tree where it is defined.
<xsl:template match="document"> <xsl:variable name="author"><xsl:value-of select="property/@author"/></xsl:variable> <xsl:for-each select="chapter"> Author: <xsl:value-of select="$author"> ... </xsl:for-each> </xsl:template>
In this code, I stored the value of attribute "author" of element "property" in variable "author". Then I used this variable in a "for-each" loop.
Last update: 2014.
Table of Contents