Is it possible to open 2 documents from an xQuery and do a join on them?
Yes, here is an example from the XQuery spec.:
"Joins, which combine data from multiple sources into a single result, are a very important type of query. In this section we will illustrate how several types of joins can be expressed in XQuery. We will base our examples on the following three documents:
parts.xml
that contains many part
elements; each part
element in turn contains partno
and description
subelements.suppliers.xml
that contains many supplier
elements; each supplier
element in turn contains suppno
and suppname
subelements.catalog.xml
that contains information about the relationships between suppliers and parts. The catalog document contains many item
elements, each of which in turn contains partno
, suppno
, and price
subelements.A conventional ("inner") join returns information from two or more related sources, as illustrated by the following example, which combines information from three documents. The example generates a "descriptive catalog" derived from the catalog document, but containing part descriptions instead of part numbers and supplier names instead of supplier numbers. The new catalog is ordered alphabetically by part description and secondarily by supplier name.*
<descriptive-catalog>
{
for $i in fn:doc("catalog.xml")/items/item,
$p in fn:doc("parts.xml")/parts/part[partno = $i/partno],
$s in fn:doc("suppliers.xml")/suppliers
/supplier[suppno = $i/suppno]
order by $p/description, $s/suppname
return
<item>
{
$p/description,
$s/suppname,
$i/price
}
</item>
}
</descriptive-catalog>
The previous query returns information only about parts that have suppliers and suppliers that have parts. An outer join is a join that preserves information from one or more of the participating sources, including elements that have no matching element in the other source. For example, a left outer join between suppliers and parts might return information about suppliers that have no matching parts."
Do note that XQuery does not have a standard document() function (it is an XSLT function) and instead has the doc() function, which is part of the "XQuery 1.0 and XPath 2.0 Functions and Operators".
There are at least two errors in the answer by Chris:
fn
" prefix.