Re: [xsl] Reference to variable cannot be resolved.

Subject: Re: [xsl] Reference to variable cannot be resolved.
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 14 Feb 2003 16:43:25 +0000
Hi Brian,

>>> Note that XSLT 2.0 (per the November 15 WD) changes this so that
>>> shadowing a local variable is not an error, though it is
>>> discouraged:
>>>
>>>   http://www.w3.org/TR/xslt20/#dt-shadows
>>
>> This is really horrible.
>> 
>> Why should a language support a practice that must be discouraged?
>
> The example given in the spec seems a bit misleading, because it
> really illustrates the restrictions of scope and not the practice of
> shadowing. The following is illegal in either spec:
>
> <xsl:template match="/">
>   <xsl:variable name="x" select="1"/>
>   <xsl:variable name="x" select="$x+1"/>
>   <xsl:value-of select="$x"/>
> </xsl:template>

Um, I think that's legal in XSLT 2.0. What makes you think that it
isn't?

I think that the argument for allowing shadowing of local variables
was that it allows those unimaginative programmers who can only think
of the one variable name (you know who I'm talking about, David C.) to
reuse that variable name while carrying out several steps of
processing on the same value, precisely as you have above.

Note that both XSLT 1.0 and XSLT 2.0 allow you to do:

<xsl:template match="/">
  <xsl:variable name="x">
    <xsl:variable name="x" select="1" />
    <xsl:value-of select="$x + 1" />
  </xsl:variable>
  <xsl:value-of select="$x" />
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread