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.
For example:
<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
Introduction of XML (eXtensible Markup Language)
DOM (Document Object Model) Programming Interface
SAX (Simple API for XML) Programming Interface
DTD (Document Type Definition) Introduction
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
Validating XML Documents Against Specified XML Schemas
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
►XSLT Elements as Programming Statements
"value-of" - Evaluating XPath String Expressions
Examples of Using "value-of" Elements
"{expression}" - Shorthand of "value-of" Elements
►"variable" - Declaring Variables
"for-each" - Looping through a Node Set
Examples of Using "for-each" Elements
"if" - The Conditional Element
"choose" - The If...Else Element
Control and Generate XML Element in the Result
XML Tools Plugin for Notepad++