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

Bracketing in the Return clause

Apart from the embedding of XQuery expressions in the return clause, a characteristic feature of the priority regulation with regard to the concatenation of individual XQuery expressions by the comma operator should be discussed. In general, a comma is set in order to combine the results of both operands in the form of a single sequence. Since the comma operator takes up the lowest level in the priority regulation, the expressions have to be bracketed accordingly when the comma operator is used in the return clause. The following example illustrates this characteristic:

for $d in fn:doc("Hochwaldklinik.xml")//Doctor
where $d/Address/City = "Berlin"
return <Doctor>{ $d/Name/LastName }</Doctor>
<Comment>A doctor from Berlin</Comment>

It should be firstly noted that the above XQuery statement is syntactically not correct, because the return clause returns (in every iteration step) two nodes being completely independent from one another – and not a node sequence – as result. This should be changed by the following modified return clause:

...
return (<Doctor>{ $d/Name/LastName }</Doctor>,
<Comment>A doctor from Berlin</Comment>)

In this example, the comma operator ensures the generation of a sequence on the level of the two elements Doctor and Comment. The bracketing additionally guarantees the correct assignment of the Comment element, so that the evaluation of the query has the following result:

<Doctor>Nauman</Doctor>
<Comment>A doctor from Berlin</Comment>

<Doctor>Shore</Doctor>
<Comment>A doctor from Berlin</Comment>

<Doctor>Meier</Doctor>
<Comment>A doctor from Berlin</Comment>

Please note that the comma operator has the lowest status in the priority regulation. If the above expression is formulated without bracketing, the comma operator will not operate within the element construction of the return clause, but on the level of the entire FLWOR expression and the element construction of the comment node and, as a consequence, lead to the following result:

<Doctor>Nauman</Doctor>
<Doctor>Shore</Doctor>
<Doctor>Meier</Doctor>
<Comment>A doctor from Berlin</Comment>

The image below outlines the situation with regard to the hierachy development of the involved XQuery expressions:

Usage of the comma operator in the return clause

Image: Usage of the comma operator in the return clause

 

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

<< backnext >>