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

Left or right outer join

The symmetry of the nested for clauses is only to be maintained if it can be ensured that there is at least one join partner for each element and thus no loss of information is generated. For example, for each patient a list of the doctors to be contacted in case of emergency is to be compiled, whereby a join of the collection of the patient documents with the XML document Hochwaldklinik.xml must be performed.

A condition is that the emergency doctor must live in the same city as the patient. The query iterates over all patients and all doctors and checks in the where clause the join predicate. In the return clause the determined combinations of patient name and emergency doctor are united.

<PatientList>
{
for $p in fn:collection("Patient")
for $d in fn:doc("Hochwaldklinik.xml")//Doctor
where $p//Address/City = $d//Address/City 
return
<Patient>
{ $p/*/Name }
<EmergencyDoctor>
{ $d/Name }
{ $d/Address }
</EmergencyDoctor>
</Patient>
}
</PatientList>

As a consequence, in the list only patients can be found who live in a city in which also at least one doctor of the hospital resides, since otherwise the where clause cannot be met. The remaining patients do not appear in the result document. In order to achieve this nevertheless, firstly the patient information must be generated in an outer FLWOR expression and after that the (possibly empty) list of the appropriate emergency doctors must be added in an inner FLWOR expression. An appropriate left or right outer join will result as follows:

 

<PatientList>
{
for $p in fn:collection("Patient")
return
<Patient>
{ $p/*/Name }
<EmergencyDoctors>
{
for $a in fn:doc("Hochwaldklinik.xml")//Doctor
where $p//Address/City = $d//Address/City
return
<EmergencyDoctor>
{ $d/Name }
{ $d/Address }
</EmergencyDoctor>
}
</EmergencyDoctors>
</Patient>
}
</PatientList>

In this case, a patient also appears in the result document if there is no doctor in his immediate proximity, i. e. in the same city.

It should be noted here that the semantics of a left or right outer join can be realised by the possibility of the nesting of XQuery expressions, in particular of FLWOR expressions, and thus corresponds to the hierarchical characteristic of XML.

 

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

<< backnext >>