XQuery/Searching multiple collections

Motivation
You want to find records in multiple collections.

Method
There are several ways to do this. The simplest way is to put both collections in a parent collection and start your search at the parent.

Lets assume you have three collections:

/db/test /db/test/a /db/test/b

To get all the books in both collection a and b just specify the parent collection which is /db/test

for $book in collection('/db/test')//book

Note that the double forward slash // will find the books anywhere in the base collection or any child collections.

If you have two collections that are at different locations in a file system you can simply specify each collection and join them together using the sequence join operation. This is the default operation of enclosing two sequences in parenthesis. For example if you have two sequences, a and b, the concatenation of the two sequences is just (a,b).

Assume you have two collections that have books in the following collections:

Collection A
File='/db/test/a/books.xml'

Collection B
File='/db/test/b/books.xml'

The following query would operate on both collections.

If you wanted to only return the titles you could use the following:

This would return the following results: