How to use XSLT Variables
If you’re not a programmer, you might assume that introducing variable into a markup language is not fair, but variables in XSLT are not much different than entitles in XML. If you can think that way, you should have no trouble with them.
There are two kinds of variable elements’ xsl: variable and xs l: param. Both are allowed at the top level (a global variable (and in individual templates of the style sheet) . An element is considered a top-level element if it is a child of the xs l: style sheet element. Bear in mind a few general rules when developing style sheet variables:
There can’t be more than one top- level variable with the same name and level of importance An XSLT processor will either deliver an error message or choose the variable that it deems most important.
Unlike programming language variables, variable values cannot be changed dynamically by some function within XSL. This can be disconcerting to programmers accustomed to statements such as “a = if x then b else c ‘’, but makes it easier to conditionally change variable values.
A variable can be bound tooneofthe four XPath data types: Boolean, string, number, or node set.
A variable can be bound to a data type exclusive of variable elements, such as a result tree fragment. You can only perform string operations on result tree fragments, and you can’t use the /, //, or [ ] operator son them. Any action performed on a variable has the same effect as it would on the equivalent node set.
Result tree fragments can only be returned by variable expressions if the variable is result tree fragments, if they are the results of expression functions that create result tree fragments, or the result of a system property whose result is a result itree fragment.
Binding with the xsl:variable element
The xsl:variable element is the simplest type of variable. It holds a value that can be referenced some where else in a document. An xsl: variable element has one required attribute-name-that you can use to name your variable.
<xsl:variable name= “colorit:>color</xsl:variable>
The element content is the value of the variable. You can reference the variable somewhere else in your document, such as within a template, as follows:
The preceding example creates a new element, called APPLE, and an attribute for that new element. Note how the attribute takes its name from the value of the previous variable. Theis creates an attribute-color-that is addigned a value of red.
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”Copyright - © 2004 - 2019 - All Rights Reserved.