• +49-(0)721-402485-12
Your experts for XML, XQuery and XML Databases

Return clause

The return clause of a FLWOR expression indicates to some extent the template according to which the result document shall be constructed. An important point to note in this context is that the dereferencing of variables by an evaluation context must explicitly be indicated in case surrounding element constructors exist, because the XQuery processor is in the static mode.

So, for example, the following query returns three times the expression <OccupationalGroups> $x </OccupationalGroups>:

for $x in (<Doctor/>, <Nurse/>, <Technician/>)
return
<OccupationalGroups> $x </OccupationalGroups>

The dereferencing is activated by the embedding of the $x variables in a new evaluation context, so that from the view of the application the correct return clause is as follows:

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

With the computed element constructor the formulation would be:

for $x in (<Doctor/>, <Nurse/>, <Technician/>)
return
element OccupationalGroups { $x }

The computed form of the element construction in the context of the return clause is useful and an extremely powerful tool if, for example, specific subelements of a currently regarded element shall not be taken over to the output. As a consequence, an anonymised list of patients (without names) can be generated by the following statement:

for $p in fn:collection("Patients")/*
order by $p/DateOfBirth
return
element AnonymisedPatient {
$p/* except $p/Name
}

Since every kind of expression may occur in an evaluation context, this implies a possible nesting of FLWOR expressions.

 

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

<< backnext >>