The xmldb module
The xmldb module (
http://exist-db.org/xquery/xmldb function namespace)
contains functions for manipulating database contents. The full list of functions and their
documentation can be found in the Function Documentation Library. This article handles some of the highlights and main
uses for this module.
The xmldb functions can be used to create new database collections or documents.
To illustrate this, suppose we have a large file containing several RDF metadata records, but, since our application expects each record to have its own document, we do not want to store the metadata records in a single file. SO we have to divide the document into smaller units. This can be done by the following XQuery:
Let's look at this example in some detail:
First, since we are using functions
xmldb:store(), which require the user to be logged in as a member of the
dbagroup, we must log in using
Once logged in, we can create a new sub-collection
for-loop iterates over all child elements of the top RDF element.
In each iteration, we use
xmldb:store()to write the current child node to a new document.
Since a unique document name is required, we need a way to generate unique names. The URI contained in the
rdf:aboutattribute is unique, so we compute an MD5 key from it, append
.xml, and use this as the document's name.
A database can contain a virtually unlimited set of collections and documents. Four
functions are available to restrict the input document set to a user-defined set of documents
xmldb:xcollection(). The first two
are standard XPath functions, the others eXist-db specific extensions.
The differences between the XPath and the eXist-db specific functions are:
doc()is restricted to a single document-URI argument,
xmldb:document()accepts multiple document paths to be included into the input node set.
xmldb:document()without an argument includes every document in the database.
collection()function specifies the collection of documents to be included in the query evaluation. By default, documents found in sub-collections of the specified collection are also included.
For example, suppose we have a collection
/db/testthat contains two sub-collections
/db/test/def. In this case, the function call
collection('/db/test')will include all of the resources found in
xmldb:xcollection()does not include sub-collections.
Without an URI scheme in front (like
interprets the arguments to
absolute or relative paths, leading to some collection or document within the database. For
This refers to a resource called
This references a resource relative to the base URI property defined in the static XQuery context. This contains an XML:DB URI pointing to the base collection (see below) for the current query context, for instance
The base collection depends on how the query context was initialized. If you call a query
via the XML:DB API, the base collection is the collection from which the query service was
obtained. All relative URLs will be resolved relative to that collection. If a stored query is
executed via REST, the base collection is the collection in which the XQuery source resides.
In most other cases, the base collection will point to the database root
As it might not always be clear what the base collection is, we recommend to always use absolute paths. This allows using the query with different interfaces.
You can also pass a full URI to the
The data on URI will be retrieved and stored in a temporary document in the database.