1

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?

1
  • 1
    The second one is local to the template. You're not overriding it in the true sense, but you are in effect making the outer version inaccessible. [Fiddle](www.xmlplayground.xyz/9Usr9c) Commented Feb 15, 2019 at 21:13

2 Answers 2

1

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 or xsl:param element within a template shadows another binding established by an xsl:variable or xsl:param element also within the template. It is not an error if a binding established by an xsl:variable or xsl:param element in a template shadows another binding established by an xsl:variable or xsl:param top-level element.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

That is not entirely correct. It is not only a matter of scope. You're not allowed to override a variable defined in the same template, even for a limited scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.