This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 30171 - Can a global variable declaration refer to an imported variable it is overriding?
Summary: Can a global variable declaration refer to an imported variable it is overrid...
Status: RESOLVED FIXED
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: XSLT 3.0 (show other bugs)
Version: Proposed Recommendation
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Michael Kay
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-25 23:21 UTC by Michael Kay
Modified: 2017-10-29 23:21 UTC (History)
0 users

See Also:


Attachments

Description Michael Kay 2017-08-25 23:21:32 UTC
Consider a stylesheet module a.xsl that contains the global variable declaration

<xsl:variable name="v" xml:id="v1" select="23"/>

Now consider a stylesheet module b.xsl that does

<xsl:import href="a.xsl">
<xsl:variable name="v"  xml:id="v2" select="$v+1"/>
<xsl:template name="xsl:initial-template">{$v}</xsl:template>

My immediate instinct is that the second variable declaration (v2) is an error: a variable is not in scope within its own declaration. But there appears to be a reading of the spec that suggests that the stylesheet should output 24.

9.5 tells us that v1 declares a global variable $v that is visible "everywhere except within its own declaration, and where it is shadowed by another binding".

In the select expression of v2, is the variable declared by v1 visible? It's not within its own declaration. Is it shadowed by another binding - specifically by the binding v2?

[Definition: 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. ]

This definition suggests that v2 shadows v1. But there is something missing in the definition. A local variable v shadows a global variable v, but only within the scope of the local variable v. The pertinent phrase is "where it is shadowed". So is v1 shadowed by v2 *within the select expression of v2*, given that v2 is not visible here? 

This question applies equally to XSLT 1.0 and 2.0. I don't think either of those specs tells us the answer.
Comment 1 Michael Kay 2017-08-26 07:46:43 UTC
I have written a test case for this situation: variable-0120.

I think it was always the WG's intent that this should be an error, and I think we should issue a clarifying erratum to say so.

(I) In 9.9 para 2, change

A global variable binding element is visible everywhere in the containing package (including other stylesheet modules) except within the xsl:variable or xsl:param element itself and any region where it is shadowed by another variable binding. 

to

A global variable binding element is visible everywhere in the containing package (including other stylesheet modules) except (a) within the xsl:variable or xsl:param element itself, (b) within any other global variable declaration binding a variable with the same name, and (c) within any region where it is shadowed by another variable binding. 

(II) Also in 9.9, change the definition of "shadows" from

[Definition: 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. ]

to

[Definition: Within a region of the stylesheet where a binding B1 is visible, B1 shadows another binding B2 having the same name as B1 if B1 occurs at a point where B2 is visible.]
Comment 2 Michael Kay 2017-10-13 20:18:50 UTC
Erratum E5 has been drafted.
Comment 3 Michael Kay 2017-10-29 23:21:40 UTC
The erratum was reviewed and accepted with one change: it should be classified as substantive rather than editorial.