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

Asymmetric notation of a left or right outer join

For the last case of the full outer join, the required XQuery query pattern is demonstrated on the example of the previous section in two steps. Firstly, the asymmetric alternative for a left or right outer join is explained, followed by the combination with a full outer join.

For this purpose, the semantics of the left or right outer join can be maintained (thereby the same result can be achieved) if the result document firstly lists all patients with their emergency doctors and in a second part, all patients without an emergency doctor are included. For the first part, the symmetric notation from the previous section can basically be adopted. For the second part, it should be taken into account for the where clause that only items are adopted which have no join partners.

 

<PatientList>
(: patients with emergency doctor :)
<PatientsWithDoctor>
{
for $p in fn:collection("Patients")
for $d in fn:doc("Hochwaldklinik.xml")//Doctor
where $p//Address/City = $d//Address/City
order by $p//LastName, $p//FirstName
return
<Patient>
{ $p/*/Name }
<EmergencyDoctor>
{ $d/Name }
{ $d/Address }
</EmergencyDoctor>
</Patient>
}
</PatientsWithDoctor>
,
(: patienten without doctor :)
<PatientsWithoutDoctor>
{
for $p in fn:collection("Patients")

where fn:empty(fn:doc("Hochwaldklinik.xml")//Doctor
[.//Address/City = $p//Address/City]
order by $p//LastName, $p//FirstName
return
<Patient>
{ $p/*/Name }
</Patient>
}
</PatientsWithoutDoctor>
</PatientList>

In order to perform the test, here the fn:empty() function is used which returns TRUE if the sequence passed on as parameter contains no element. Concretely in this example, the result of the path expression which returns the emergency doctors for the specific patient is passed on.

It should be noted for this query pattern that the two elements PatientsWithDoctor and PatientsWithoutDoctor stand on the same level and built a sequence from the point of view of the root element PatientList. This also implies the usage of a comma operator between the two FLWOR expressions.

In case the bisection of the result document implied by this asymmetric notation shall be eliminated, the complex FLWOR expression is to be embedded into a surrounding FLWOR expression taking over the final and uniform sorting. For the current example, the following XQuery statement would arise as a result:

<PatientList>
{
for $x in { (: above XQuery statement without order by clauses :) }
order by $x//LastName, $x//FirstName
return $x
}
</PatientList>

 

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

<< backnext >>