"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

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

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 Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version