Next page Previous page Start of chapter End of chapter

XML Query Language

The XML query language (XQuery) is a complete query language for XML databases. It stands to XML databases as SQL stands to relational ones. An XML database is a collection of (related) XML documents.

XQuery works on sequences, not on node sets as XPath. A sequence contains items which are either XML nodes (such as elements and attributes) or atomic values (like strings and numbers). The relationship between XPath and XQuery consists in the fact that XPath expressions are used in XQuery queries (xqueries). Hence, we may consider XPath as a syntactic fragment of XQuery.

A typical xquery works as follows:

  1. load: one or more XML documents are loaded from the database;
  2. retrieve: XPath expressions are used to retrieve sequences of tree nodes from the loaded documents;
  3. process: the retrieved node sequences are processed with XQuery operations like filtering (creating a new sequence by selecting some of the items of the original one) and ordering (sorting the items of the sequence according to some criteria);
  4. construct: new sequences may be constructed and combined with the retrieved ones;
  5. output: a final sequence is returned as output.

Some of the above steps are optional. For instance, an xquery may avoid the loading of a document and the retrieval of sequences with XPath expressions, and, instead, it may create, process, and output its own sequences. In fact, XQuery can be used as a (Turing-complete) programming language that manipulates item sequences, possibly using functions defined by the user. In these pages we will focus on XQuery as an XML query language.

An XQuery processor is a software that evaluates xqueries. See the XQuery resources page for a list of XQuery processors. Saxon is a good example. In order to evaluate with Saxon the query contained in the file xquery.xql, type the following:

java net.sf.saxon.Query xquery.xql
Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet