[xquery-talk] variable references in location steps

Jens Teubner jens.teubner at inf.ethz.ch
Fri Feb 25 09:16:49 PST 2011

On Fri, Feb 25, 2011 at 02:17:13AM +0100, Ron Van den Branden wrote:

> let $test :=
>   <test>
>     <a><el1/></a>
>     <b><el2/></b>
>   </test>
> let $el1 := $test//el1
> let $el2 := $test//el2
> return
>   <result>
>     <step>
>       <el1.a>{$test//a//$el1}</el1.a>
> [...]


I guess what is confusing you here is the semantics of the (binary) ‘/’
operator in XQuery.  ‘/’ does not itself evaluate any XPath axes.
Rather, it establishes the proper context for successive XPath
navigation steps.  In fact, ‘/’ is almost like a ‘for’ iteration
primitive, only that it has special typing rules and that it does
duplicate elimination afterward.

To illustrate,

  let $foo := (<a/>, <b/>, <c/>)

will return (42, 42, 42), because you iterate over $foo (three
iterations) and for each iteration 42 is returned.

Conversely, in

  let $foo := (<a/>, <b/>, <c/>)
  let $bar := (<x/>, <y/>)

you iterate over $bar (i.e., two iterations), and for each iteration
$foo is returned (think of (<a/>, <b/>, <c/>, <a/>, <b/>, <c/>) as an
intermediate result).  Now, since $foo is a list of nodes, the ‘/’
operator performs duplicate elimination and the result of this query is
(<a/>, <b/>, <c/>).

Now to your query: You seem to be interested only in the tag name of the
nodes in your variables.  What you could try is something like

  <el2.a>{$test//a//*[name() = $el2/name()]}</el2.a>

(sorry, I have abused the ‘/’ here myself).

Best regards,


(I didn't actually run any of the above queries.  Please bear with me if
they produce errors.)

Jens Teubner
ETH Zurich, Systems Group
Universitaetstrasse 6 / CAB E 77.1
8092 Zurich, Switzerland

                 XQuery processing at the speed of light: MonetDB/XQuery
       http://www.monetdb-xquery.org/  http://www.pathfinder-xquery.org/

More information about the talk mailing list