- Tech know how online

XML query language (XQuery)

XQuery is a language that can be used to make queries to XML documents and generate result documents. XQuery contains the XPath 2.0 language as a subset. Comparable in performance to the data query language SQL, XQuery can be used to issue complex queries to XML documents. XQuery is a declarative language and is syntactically oriented to the Structured Query Language (SQL), which also optimizes these algebraically based expressions over the elaborate path specifications of XPath.

XQuery is a query language that does not have the limitations of XPath or XSLT. XQuery has been standardized by the World Wide Web Consortium (W3C) since 2007, with various language stems such as Quilt, YATL, or XML-QL incorporated into the W3C's recommendation in addition to XPath. A special aspect of XQuery is that query processing such as the order of evaluation, access paths or optimizing mechanisms like index structures are completely handled by XQuery itself - the user only defines conditions in advance and then gets a valid result. This is another reason why XQuery is an alternative to XLST (XSL Transformation) for some use cases, since the structure of a result document can also be specified in the XQuery request.

XQuery as a functional query language

XQuery is defined as a functional query language - without consideration of higher-order functions - and was designed, among other things, to complement the approach implemented with XPath 2.0 for identifying arbitrary sets of nodes within XML documents. For example, XPath requires that:

  • That the construction of custom result types is not possible, and as a result the result is always an element or attribute set.
  • The structure of the XML input document must be known beforehand.
  • Path expressions are often complex and confusing.
In addition to overcoming these drawbacks, XQuery now implements the following requirements:

  • The result of an XQuery query is always a completed XML document.
  • The functions of XQuery can be combined arbitrarily.
  • Strict typing is supported by the XML schema
  • Operations can be applied to all data types defined by the XML schema.
  • No side effects, for example on adjacent data, may occur in a query.
  • Processing of data-oriented and document-oriented documents.
  • The extraction of configuration files.
  • The use of XQuery in the context of the Document Object Model (DOM).
  • The queries to native XML repositories.
  • The support of catalog search.
  • The embedding in different syntax environments.
  • Filtering and cleansing of data streams.
  • Querying XML documents so that binary documents can be generated again from XML data.

Data model of XPath and XQuery

XPath 2.0 and XQuery are based on a common data model - the XDM - whose components are nodes, atomic values and sequences. An XML document is converted into a tree of nodes. Analogous to the information units existing in an XML document, a distinction is made between up to seven different node types. Sequences are composed of atomic values or nodes and form an expression in XQuery. Likewise, all functions of XQuery work on the basis of sequences. The central expression in XQuery is the FLWOR expression, which can be used to formulate complex expressions in XQuery. This is used to make queries to an XML document as well as to support the generation of new XML documents. FLWOR is an acronym - composed of for, let, where, order by and return - and has a similar position as a SELECT-FROM-WHERE query in a SQL database.

XQuery already provides the user with a convenient set of functions and operation to map the power of expressions and their functionality. Furthermore, XQuery provides the ability for the user to define functions, and functions must always reside in a namespace.

XQuery can process data from different input sources. In addition to those derived from the above-mentioned requirements, this also includes, for example, parts of XML documents that are requested via the Web using a URI, a collection of documents from documents, or data that is stored in a relational database with an XML front end.

Example of an XQuery request

Example of an XQuery request

A so-called XQuery processor is a program that parses, analyzes and evaluates queries. An XQuery query is evaluated within a context. The context of an expression are those statements that are used to parse and evaluate the expression. XQuery differentiates between static and dynamic contexts. XQuery processors are available as open source as well as commercially. The W3C provides a list of these processors at the link below.

Informationen zum Artikel
Englisch: XML query language - XQuery
Updated at: 29.10.2013
#Words: 1154