About
There are two elements that can be used to bind variables:
- xsl:variable
- and xsl:param.
The difference is that the value specified on the xsl:param variable is only a default value for the binding;
Parameters and variables can have their value specified either by:
- a select clause, which lets you use XPath expressions,
- or by the content of the element, which lets you use XSLT tags.
Articles Related
Syntax
<xsl:variable name="x"/>
<xsl:variable name="x" select="''"/>
where:
- name is the variable or parameter name
- select defines the default value (when not specified the default is en empty string ''
DataType
- a nodeset
<xsl:param name="resultPath" select="result"/>
- a string
<xsl:param name="resultPath" select="'result'"/>
How to
Use it in an node attribute
You can use a variable or parameter in an attribute value of a node with the curly brace
<xsl:template name="myTemplateVar">
<a href="{$href}">Test</a>
</xsl:template>
Scope
The value of a variable is known only within the scope of the current template or
]] tag (for example) in which it is defined.You cannot pass a value from one template to another, or even from an enclosed part of a template to another part of the same template.
when you code an
tag to change the value of the variable, the value is known only within the context of the tag. Once </xsl:if> is encountered, any change to the variable's setting is lost.These statements are true even for a “global” variable. You can change its value in a template, but the change applies only to that template. And when the expression used to define the global variable is evaluated, that evaluation takes place in the context of the structure's root node. In other words, global variables are essentially runtime constants. Those constants can be useful for changing the behaviour of a template, especially when coupled with include and import statements. But variables are not a general-purpose data-management mechanism.