[xquery-talk] variable references in location steps

Ron Van den Branden ron.vandenbranden at kantl.be
Fri Feb 25 02:17:13 PST 2011


I'm trying to optimize a complex XQuery expression by defining variables 
for reused context nodes. Until recently, I assumed that those variable 
references could be used in location steps as substitution for the nodes 
they hold, but apparently it doesn't work exactly like that. I'll try to 
clarify with following XQuery (note: I realize this question is 
XPath/XSLT related, but since the practical angle is XQuery-oriented, 
I'll post it here.):

let $test :=
let $el1 := $test//el1
let $el2 := $test//el2

Developing against the eXist XML database, I assumed that the queries in 
<el1.a> and <el2.b> would only return the <el1> and <el2> elements in 
their respective contexts, while <el2.a> and <el1.b> would return 
nothing. Instead, they all return the respective <el1> and <el2> 
elements, even for context nodes in which they don't occur (see <el2.a> 
and <el1.b>). Later I found out that the variable used in this last 
location step doesn't resolve to the nodes it contains, but instead is 
interpreted as a filter, which consequently returns the entire nodeset 
held in the $el1 and $el2 variables (see Jeni Tennison's clarification 
at <http://markmail.org/message/hss4auv23t6y2ep2>). Note: my assumption 
came from my experience with the eXist XML database to filter down 
ancestor nodes containing other nodes by means of a variable reference 
in a predicate: see the queries in the <predicate> section. In eXist, 
only <a.el1> and <b.el2> return the respective <a> and <b> elements, 
while <a.el2> and <b.el1> are empty, which suggests that in this case 
the variable reference is resolved to the nodes it contains. However, 
this behaviour apparently differs from Saxon's which also returns the 
<a> and <b> nodes in <a.el2> and <b.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?

Any help much appreciated!

Kind regards,

Ron Van den Branden

Ron Van den Branden
Wetenschappelijk attaché / Senior Researcher
Reviews Editor LLC. The Journal of Digital Scholarship in the Humanities

Centrum voor Teksteditie en Bronnenstudie - CTB (KANTL)
Centre for Scholarly Editing and Document Studies
Koninklijke Academie voor Nederlandse Taal- en Letterkunde
Royal Academy of Dutch Language and Literature
Koningstraat 18 / b-9000 Gent / Belgium
tel: +32 9 265 93 51 / fax: +32 9 265 93 49
E-mail : ron.vandenbranden at kantl.be

More information about the talk mailing list