• +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:

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

<OccupationalGroups>
<SerNo>1</SerNo><Doctor/>
<SerNo>2</SerNo><Nurse/> 

</OccupationalGroups>

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/>)
returnreturn
<Assignment><Assignment>
<Occupation>{$i}</Occupation><Occupation>{$i}</Occupation>
<Location>{$j}</Location><Location>{$j}</Location>
</Assignment></Assignment>

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

<Assignment><Assignment>
<Occupation>1</Occupation><Location>1</Location><Occupation>1</Occupation>Location>1</Location>
<Occupation>1</Occupation><Location>2</Location><Occupation>2</Occupation>Location>1</Location>
<Occupation>2</Occupation><Location>1</Location><Occupation>1</Occupation>Location>2</Location>
<Occupation>2</Occupation><Location>2</Location><Occupation>2</Occupation>Location>2</Location>
</Assignment></Assignment>

 

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

<< backnext >>