[xquery-talk] variable references in location steps

Liam R E Quin liam at w3.org
Thu Feb 24 22:45:34 PST 2011

On Fri, 2011-02-25 at 02:17 +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>

Erm, $el1 is already a list of nodes, so what could this possibly mean?

Do you mean you only want elements that are descendents of a node in
$test and that have an a ancestor somewhere, but that are also in $el1?
I.e. are you trying to use intersect?

Maybe you want $test//a//* intersect $el1 in that case.

Or, $el1[ancestor::a] might be more efficient, depending on the query

>        <a.el1>{$test//a[.//$el1]}</a.el1>

This is asking for all a elements anywhere in test such that the "a"
element has at least one descendent in $el1.

> Assuming Saxon is right, this makes me wonder: is my thinking based on 
> an incorrect implementation, or *is* there a way to make use of variable 
> references for filtering down nodesets in location steps?

I think the first question is, why are you trying to do this at all. Is
it for performance reasons?  The XQuery processor should notice common
sub-expressions automatically.  It may help the processor to notice the
common nodes if you avoid "//" where / will do, of course.

You probably want something like,
    $test//a//* intersect $e1

Unless you have severe performance problems, go for clarity over

If you have performance problems, I'd suggest starting out by making
sure you have indexes, and then (as it sounds as if you might be doing)
comparing e.g. saxon, basex, qizx and exist.


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Sometimes a blog at http://www.barefootliam.org/

More information about the talk mailing list