XQuery multiple xml files?

Is it possible to open 2 documents from xQuery and make a union for them?

+6
xml xquery
source share
3 answers

Yes, here is an example from the XQuery specification :.

“Joins that combine data from multiple sources into one result are a very important type of query. In this section, we illustrate how several types of joins can be expressed in XQuery. We will base our examples on the following three documents:

  • A document called parts.xml containing many part elements; each part element in turn contains partno and description subelements.
  • A document called suppliers.xml that contains many supplier elements; each supplier element, in turn, contains suppno and suppname subelements.
  • A document called catalog.xml that contains information about the relationship between suppliers and parts. A catalog document contains many item elements, each of which, in turn, contains partno , suppno and price subelements.

A regular (“internal”) join returns information from two or more related sources, as shown in the following example, which combines information from three documents. The example creates a “descriptive catalog”, obtained from a catalog document, but containing part descriptions instead of part numbers and supplier names instead of supplier numbers. The new catalog is sorted alphabetically by the description of the parts and, secondly, by the name of the supplier. *

 <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. external join is a union that stores information from one or more participating sources, including elements that do not have a corresponding element in another source. For example, a left outer join between suppliers and parts may return information about suppliers that do not have matching parts.

Note that XQuery does not have a standard document () function (this is an XSLT function ) and instead has a doc () function, which is part of " XQuery 1.0 and XPath 2.0 Functions and Operators ".

Chris's answer has at least two errors:

  • XQuery is case sensitive - the headwords used in Chris's example will not be resolved by the corresponding XQuery.
  • There is no need to prefix standard functions , such as doc (), I just quote the XQuery specification, which has a prefix. Otherwise, in my own code, I would omit the prefix " fn ".
  • Function document () is not a standard XQuery / XPath function . Instead, use doc () .
+9
source share

In XQuery, if you write something like the following:

 for $x in doc('doc1.xml')//a for $y in doc('doc2.xml')//a where $x/@name = $y/@name return $x 

then your XQuery processor must be smart enough to determine if it is a connection.

In XQuery, you do not explicitly indicate that something is a join. A common theme in XQuery is that your program says that the information you want and not how to calculate it.

Although it might seem like you are repeatedly sorting through a second document in practice, the real XQuery processor will do it more intelligently, roughly the same as the following SQL statement (my SQL is very rusty, so I apologize if this syntax is completely wrong)

 SELECT doc1.a FROM doc1 INNER JOIN doc2 WHERE doc1.name = doc2.name 

The XMark test contains several sample queries that are worth a look. In particular, requests 9 through 12 perform joins.

0
source share

it is simpler (at least using SAXON):

 let $items := ( doc("file1.xml") , doc("file2.xml") , doc("file3.xml") ) for $x in $items ... 
0
source share

All Articles