RE: Variable references in path expressions

> I have some questions about variable references appearing in path 
> expressions and, in particular, how node identity, variable 
> binding, and 
> element constructors actually "work" in this context. 
> Insights into this 
> would be appreciated!

These are good questions and in general you have come to the right answers.
> 
> Consider the path expression $a/$b. 
> 
> If $a is a singleton, then the expression is equivalent to $b.

Not quite, because "/" causes nodes to be reordered and deduplicated. For
example given the context node

<book><author/><title/></book>

the result of
let $a := ., $b := (title, author)
return $a/$b

consists of (author, title).  
> 
> However, if $a is not a singleton, then a query such as
> 
>     let
>       $a := (<a1/>, <a2/>, <a3/>),
>       $b := (1, 2, 3)
>     return
>       $a/$b
> 
> will return
> 
>     (1, 2, 3, 1, 2, 3, 1, 2, 3)
> 
> determined by evaluating $b in the context of each node from $a, and 
> concatenating the resulting sequences of atomic values.

Yes.
> 
> When combining node sequences obtained by evaluating a step in a path 
> expression, however, duplicate nodes are eliminated based on node 
> identity. According to the XQuery 1.0 and XPath 2.0 Data 
> Model, atomic 
> values do not have identity, which is why they are simply 
> concatenated.

Yes.
> 
> However, will
> 
>     let
>       $a := (<a1/>, <a2/>, <a3/>),
>       $b := (<b1/>, <b2/>, <b3/>)
>     return
>       $a/$b
> 
> return
> 
>     (<b1/>, <b2/>, <b3/>, <b1/>, <b2/>, <b3/>, <b1/>, <b2/>, <b3/>)
> 
> because the expression bound to $b constructs unique elements 
> each time it is evaluated? 

Yes.

> Or should (<b1/>, <b2/>, <b3/>) be returned? 

No, this would be incorrect. (I can't point you to a discussion of this in
the spec; I think the formal semantics handles the issue. But it's certainly
been discussed by the WG and it's my clear understanding that the language
requires this result.)

> This raises another question: do constructed nodes 
> even have 
> an identity, or does the concept apply only to nodes from a document?
> 

Yes, all nodes have an identity.

Michael Kay
http://www.saxonica.com/

Received on Tuesday, 26 April 2005 08:35:47 UTC