XQuery

From Free net encyclopedia

XQuery is a query language (with some programming language features) that is designed to query collections of XML data. It is semantically similar to SQL.

XQuery 1.0 is being developed by the XML Query working group of the W3C. The work is closely coordinated with the development of XSLT 2.0 by the XSL Working Group; the two groups share responsibility for XPath 2.0, which is a subset of XQuery 1.0. XQuery 1.0 became a W3C Candidate Recommendation on November 3, 2005.

Contents

Features

XQuery provides the means to extract and manipulate data from XML documents or any data source that can be viewed as XML, such as relational databases or office documents.

XQuery uses XPath expression syntax to address specific parts of an XML document. It supplements this with a SQL-like "FLWOR expression" for performing joins. A FLWOR expression is constructed from the five clauses after which it is named: FOR, LET, WHERE, ORDER BY, RETURN.

The language also provides syntax allowing new XML documents to be constructed. Where the element and attribute names are known in advance, an XML-like syntax can be used; in other cases, expressions referred to as dynamic node constructors are available. All these constructs are defined as expressions within the language, and can be arbitrarily nested.

The language is based on a tree-structured model of the information content of an XML document, containing seven kinds of node: document nodes, elements, attributes, text nodes, comments, processing instructions, and namespaces.

The type system of the language models all values as sequences (a singleton value is considered to be a sequence of length one). The items in a sequence can either be nodes or atomic values. Atomic values may be integers, strings, booleans, and so on: the full list of types is based on the primitive types defined in XML Schema.

XQuery 1.0 does not include features for updating XML documents or databases. It also lacks full text search capability. These features are both under active development for a subsequent version of the language.

Examples

The sample XQuery code below lists the unique speakers in each act of Shakespeare's play Hamlet.

<html><head/><body>
{
  for $act in doc("hamlet.xml")//ACT
  let $speakers := distinct-values($act//SPEAKER)
  return
    <span>
      <h1>{ $act/TITLE/text() }</h1>
      <ul>
      {
        for $speaker in $speakers
        return <li>{ $speaker }</li>
      }
      </ul>
    </span>
}
</body></html>

XQuery is a functional language consisting entirely of expressions. There are no statements, even though some of the keywords appear to suggest statement-like behaviors. To execute a function, the expression within the body gets evaluated and its value returned. Thus to write a function to double an input value, you simply write:

declare function local:doubler($x) { $x * 2 }

To write a full query that says Hello World you write the expression:

"Hello World"

Further information

  • XQuery from the Experts: A Guide to the W3C XML Query Language. Howard Katz (ed). Addison-Wesley, 2004. ISBN 0-321-18060-7
  • An Introduction to the XQuery FLWOR Expression. Dr. Michael Kay (W3C XQuery Committee), 2005.

Implementations

External links


Portions borrowed with permission from the book "XML Hacks" (O'Reilly Media).

Previous version based on an article at the French language Wikipedia.de:XQuery fr:XML Query nl:XQuery pl:XQuery it:XQuery tr:XQuery