[xquery-talk] Re: Variable references and prolog grammar

Pierrick Brihaye pierrick.brihaye at culture.gouv.fr
Fri Aug 12 14:22:30 PDT 2005

Michael Kay <mhk at ...> writes:

> See section 4.15 of the spec:
> The static context for a function body includes all functions that are
> declared or imported anywhere in the Prolog, but it includes only those
> variables and namespaces that are declared or imported earlier in the Prolog
> than the function that is being defined.
> XQuery differs here from XSLT, which allows forwards references to global
> variables.

Thank you Michael, I was considering the problem by the wrong end, I mean...
4.14 :-)

On the same topic, I was trying to understand why I didn't get an error with :

declare variable $foo := "foo1";
let $foo := "foo2"

... which returns "foo2".

I was wrongly thinking about a name collision between the 2 variables but the
specs are clear :

>The term variable declaration always refers to a declaration of a variable in a
>Prolog. The binding of a variable to a value in a query expression, such as a
>FLWOR expression, is known as a variable binding, and does not make the
>variable visible to an importing module.

So, we have 2 differents things there, a variable declaration and a variable
binding, even though they are referenced in the same way (that's what makes the
thing powerful ;-)

Now, I was thinking about a another potential name collision :

let $foo := "foo1"
let $foo := "foo2"

... but it returns "foo2"

Indeed, the specs are clear :

>The for and let clauses of a given FLWOR expression may bind the same variable
>name more than once. In this case, each new binding occludes the previous one,
>which becomes inaccessible in the remainder of the FLWOR expression.

Should we bind the "same" variable in a subsequent part of the FLWOR like in :

let $foo := "foo1"
for $bar in (1 to 1)
  let $foo := "foo2"

... it returns "foo2" because the specs have yet another clear answer :

>If a variable reference matches two or more variable bindings that are in
>scope, then the reference is taken as referring to the inner binding, that is,
>the one whose scope is smaller

Now things are very clear :

The order of the declarations in the prolog is significant
It is impossible to have a duplicate variable bindings.

The exact contrary of what I could have thought this morning :-)

Thank you again.


More information about the talk mailing list