Reputation: 21725
My understanding is that <xsl:variable>
is immutable and cannot be reassigned.
I am new to XSL and came across a situation like what is in the example below.
<xsl:stylesheet>
<xsl:variable name="temp" select="true()"/>
<xsl:template name="example">
<xsl:variable name="temp" select="false()"/>
<p><xsl:value-of select="$temp"/></p>
</xsl:template>
</styleheet>
I have not found anything definitive as to why this occurs. The only way I can reason that I'm not getting an error and why temp
will output false
is that there is a global temp
variable AND an a local temp
variable (and somehow are not colliding).
Why am I able to "reassign" temp
?
Upvotes: 1
Views: 56
Reputation: 29052
My understanding is that is immutable and cannot be reassigned.
That assumption is correct. XSLT is a functional language and immutable variables are common in this type of language.
Why am I able to "reassign" temp?
The reason that happens is defined by the term Scope. This means that your second definition of the variable temp
overrides/overlays your first one - unless you leave its scope which is - here - the template. But here (as mentioned by @michael.hor257k in the comments) the scope can only be stylesheet-wide or template-wide - so a redefinition of the variable in the same xsl:template
- even in another code block - is prohibited.
The only way I can reason that I'm not getting an error and why temp will output false is that there is a global temp variable AND an a local temp variable
In fact, both variables are local, but on another level. The first definition is at the xsl:stylesheet
level and the second one at the xsl:template
level. The first one may be considered a global
variable, but that's only a matter of definition.
Upvotes: 1
Reputation: 117165
You are able to "reassign" (more precisely, to shadow) the variable, because the first binding is at the top-level of the stylesheet, while the second one is in a template.
From XSLT 1.0 specification:
A binding shadows another binding if the binding occurs at a point where the other binding is visible, and the bindings have the same name. It is an error if a binding established by an
xsl:variable
orxsl:param
element within a template shadows another binding established by anxsl:variable
orxsl:param
element also within the template. It is not an error if a binding established by anxsl:variable
orxsl:param
element in a template shadows another binding established by anxsl:variable
orxsl:param
top-level element.
Upvotes: 1