xsl-list
[Top] [All Lists]

RE: Reference to variable cannot be resolved.

2003-02-14 10:18:21
From: Jeni Tennison [mailto:jeni(_at_)jenitennison(_dot_)com]
Sent: Friday, February 14, 2003 9:43 AM
Subject: Re: [xsl] Reference to variable cannot be resolved.

Hi Brian,

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?

Uh . . . because I didn't read the following paragraph in Section 9.7 very
closely?  ;-)

"It is also not an error if a binding established by a local xsl:variable or
xsl:param element shadows another binding established by another local
xsl:variable or xsl:param. However, such shadowing is discouraged and
implementations may output a warning when it occurs."

However, this illustrates why I think the code example given is misleading,
because it doesn't really illustrate the concept of shadowing a local
variable, but rather the restrictions placed on its scope: the variable
declaration inside the for-each will never override the outer variable with
the same name.

Still, I'm very surprised that my above example would be legal, and it makes
Dimitre's objection that much clearer: if the value of $x can't be changed
by a subsequent binding within the same scope, then why allow the practice
at all?

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>

True . . . but it does makes sense, since the inner $x is not in the same
scope as the outer $x.  What I'm struggling to understand is why the spec
would allow a programmer even to attempt to assign a new value to $x.

cheers,
b.

| brian martinez                              
brian(_dot_)martinez(_at_)trip(_dot_)com |
| senior gui programmer                                  303.708.7248 |
| trip network, inc.                                 fax 303.790.9350 |
| 6436 s. racine cir.                             englewood, co 80111 |
| http://www.cheaptickets.com/                   http://www.trip.com/ |

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