• +49-(0)721-402485-12
Ihre Experten für XML, XQuery und XML-Datenbanken

Binding of several variables

In a FLWOR expression it is possible to bind simultaneously several variables by a let or a for instruction.

let $x := ..., $y := ...for $x in ..., $y in ...

is equivalent to:

let $x := ...for $x in ...
let $y := ...for $y in ...

However, it should be noted that the order of the binding without further explicit sorting by the order by clause has influence on the call order of the return operator. The earlier the binding is performed, the stronger is the influence of the variables on the output order when generating the result. Furthermore, variables can be defined depending on already declared variables, for example:

let $x := fn:doc("Hochwaldklinik.xml")
for $y in $x//Doctor,
$z in $x//Ward

The effects of the binding moment can be well demonstrated on the basis of the concept of the position variables within a for clause. A position variable – by default of xs:integer type – can optionally be indicated with the key word at when bound to a varibale and reflects the index of the element currently processed or the number of the respective run. The following FLWOR expression returns for each item the corresponding position in the result:

for $x at $i in (<Doctor/>, <Nurse/>)
return (<SerNo>{ $i }</SerNo>,$x)



The effects of different arrangements of for clauses can now be illustrated with the help of the position variables. The following exemplary expressions form pairs of occupations and locations, whereby the variable introduced first is dominant in relation to the variables later bound:

for $x at $i infor $y at $j in
(<Doctor/>, <Nurse/>)(<Laboratory/>, <Ward/>)
for $y at $j infor $x at $i in
(<Laboratory/>, <Ward/>)(<Doctor/>, <Nurse/>)

The results of the two queries with exchanged for clauses differ in the oder of the pair formation:



Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>