from Wikipedia, the free encyclopedia
Paradigms : XML
Publishing year: 2005-04-04
Developer: World Wide Web Consortium
Current  version : 3.1   (March 21, 2017)
Typing : strong
Important implementations : BaseX , EXist (database) , Galax, Saxon , Pathfinder, XQilla, Zorba
Standardizations: W3C specifications
Influenced by: XQL , XML-QL , Quilt
Operating system : platform independent

XQuery stands for XML Query Language and describes a query language specified by the W3C for XML databases . It is used to pick out individual parts from large XML data collections. In contrast, XSLT is used to transform entire XML documents.

XQuery uses a syntax based on XSLT, SQL and C and uses XPath and XML Schema for its data model and its function library . It emerged from the languages XQL , XML-QL and Quilt .

XQuery is strongly typed and Turing-complete .

Donald D. Chamberlin played a key role in the development (as with Quilt) .

Language elements

In addition to path expressions ( XPath ), there are a number of other language properties that will be explained in the following sections using brief examples.

Data model

The basic data structure in XQuery is a sequence. A sequence is an ordered list of zero, one or more elements. A sequence can therefore also be an XML document. Sequences are usually bracketed and can also contain duplicates. Sequences cannot be nested.

The following sequences are identical:

  • (1, 2, 1) and (1, (2, 1))
  • (1, (), ) and (1, )
  • (<A/>) and <A/>

XQuery provides six functions for querying cardinalities on sequences:

  • fn: zero-or-one ($ seq)
  • fn: one-or-more ($ seq)
  • fn: exactly-one ($ seq)
  • fn: empty ($ seq)
  • fn: exists ($ seq)
  • fn: count ($ seq)

In XQuery, variables are identified with a $ prefix.

Editing of sequences

  • The comma operator hangs two sequences in a row
  • You can add values ​​with the function fn: insert-before
  • You can delete values ​​with the function fn: remove
  • You can rearrange values ​​with the function fn: reverse
  • You can rearrange values ​​with the function fn: unordered

Construction of XML elements

Direct XML construction with constant element names ("direct constructors"):

   <head> { $page/head/content() } </head>
   <body bgcolor={ $style/bgcolor/text() } > { $page/body/content() } </body>

"Computed constructors"

 element html {
   element head { $page/head/content() }
   element body {
        attribute bgcolor { $style/bgcolor/text() },
        text { $page/body/content() }

Both the direct notation as XML and the more declarative construction using the so-called “computed constructors” are available for constructing XML data. In both cases, the curly braces are used { ... }to embed any other XQuery expressions in the constructor.

Sort functionality ("order by")

In contrast to relational algebra and SQL , XML data elements have an implicitly specified order (“document order”). All XQuery expressions must have this order, unless this is explicitly switched off in the expression (Ordering mode: unordered).

FLWOR expressions

The so-called FLWOR expressions (pronounced: flower ) play a central role in XQuery . FLWOR is an abbreviation for the constructs for, let, where, order byand return, and can be viewed as an analogy to the (SELECT, FROM, WHERE) constructs in SQL. In contrast to SQL, however, FLWOR expressions are case-sensitive . FLWOR expressions map sequences to sequences (see data model ). FLWOR expressions have the following generic form:

for $forvar1 at $posvar1 in <Expr>, $forvar2 at $posvar2 in <Expr> …
let $letvar1 := <Expr>, $letvar2 := <Expr> …
where <BoolExpr>
order by <Expr> ascending/descending
return <Expr>

Here stands <Expr>for any other XQuery expression, and <BoolExpr>for an expression of the type boolean. The forconstructs bind their variables $forvar1, $forvar2, ... sequentially to a value from the binding sequences <Expr>. The atposition of the preceding variable in the <Expr>sequence can be linked to a position variable using the keyword , with the numbering starting at 1. In contrast, the letconstructs bind their variables $letvar1, $letvar2, ...to the entire result of the associated expression. The resulting tuple sequence contains all possible combinations of variable bindings that can be formed from the assignment of the for and let variables.

The whereconstruct is used to eliminate unwanted tuples. It makes sense to use its Boolean expression to refer to at least one of the variables bound in the forand letconstructs. The same applies to the order byconstruct used to sort the tuples. Finally, in the returnconstruct, the variables of interest are returned, possibly embedded in directly or indirectly constructed elements. In more complex cases, FLWOR expressions are also nested, which means that <Expr>any FLWOR expression can be used instead of each occurrence of .

The following example illustrates the use of forand let. The forpart of the printout selects all paragraphs within an HTML page (<p> elements). In the letpart, the number of words is determined for each paragraph with the help of predefined functions. Then all non-empty paragraphs (word count> 0) are extended by a size specification and output.

for $par in $page//p
let $words := fn:count(fn:tokenize($par/content(), " \n\t"))
where $words gt 0
return <p>
          Size: { $words }

It should be noted with this expression that the forand letpart are dependent on each other, the forvariable declared $parin the letpart is used in the part. For the evaluation, this means that the let-part $parmust be re-evaluated for each assignment of the variable .

Compound operations and groupings

Compound operations (or joins ) denote a set operation consisting of the formation of a Cartesian product of two input sets and a subsequent selection (see Relational Algebra ). Such a compound operation is already shown in the example of the previous section on FLWOR expressions. The linkages of the variables declared in the forand letparts form the respective input wherequantities to whose Cartesian product the selection conditions of the part are applied. Since selection conditions can occur both in the where-part of the expression and within the for- and let-part in subexpressions, normalization is necessary before evaluating an expression.

Predefined functions

For calculating arithmetic equations and date calculations as well as for processing sequences in the data model .

Custom functions

User- declare functiondefined functions can be declared using. The general form looks like this:

declare function namespace:funktionsname
 ($parameter1 as datentyp1, $parameter2 as datentyp2, …) as rückgabedatentyp {

Functions can recursively call other functions and themselves , so XQuery is Turing complete .

Different comparison operators

XQuery knows two types of comparison functions: value qualifying or existence qualifying. The value-qualifying operators are similar to the usual operators from other programming languages. The following operators are defined

  • eq: Checks for equality
  • ne: Checks for inequality (not equal)
  • lt: Checks for less than
  • gt: Checks for greater than
  • le: Checks for less or equal
  • ge: Checks for greater or equal

The "normal" comparison operators ( =, !=, <, >, <=, >=) through an existential comparison. There can be sequences to the left and right of the operator. So the expression is seq1 OP seq2only true if an element e1of seq1and e2from seq2there to e1 OP e2where OPone of the operators =, !=, <, >, <=or >=is. So is (1, 2) > (3, 0)true there 1 > 0is.


Counting all question XML elements in the source document:


More complex example, listing all questions of all documents in a gallery for KEduca that have less than two answers:

  for $s in fn:doc("lpi101.edugallery")//server/@address
  for $d in fn:doc($s)[count(.//question/(true, false)) <= 1]
  return <doc src="{ $s }">{
    $d//question[count((true, false)) <= 1]

Another example (shows the possibility of indirect restructuring through XQuery)

  • Source document:
<?xml version="1.0" encoding="ISO-8859-1"?>
  <part partid="0" name="car"/>
  <part partid="1" partof="0" name="engine"/>
  <part partid="2" partof="0" name="door"/>
  <part partid="3" partof="1" name="piston"/>
  <part partid="4" partof="2" name="window"/>
  <part partid="5" partof="2" name="lock"/>
  <part partid="10" name="skateboard"/>
  <part partid="11" partof="10" name="board"/>
  <part partid="12" partof="10" name="wheel"/>
  <part partid="20" name="canoe"/>
  • Target document:
  <part partid="0" name="car">
    <part partid="1" name="engine">
      <part partid="3" name="piston"/>
    <part partid="2" name="door">
      <part partid="4" name="window"/>
      <part partid="5" name="lock"/>
  <part partid="10" name="skateboard">
    <part partid="11" name="board"/>
    <part partid="12" name="wheel"/>
  <part partid="20" name="canoe"/>
  • Solution in XQuery:
declare function local:one_level ($p as node()) as node()
  <part partid="{$p/@partid}" name="{$p/@name}">
    for $s in doc("data/parts-data.xml")//part
    where $s/@partof =$p/@partid
    return local:one_level($s)
  for $p in doc("data/parts-data.xml")//part [empty(@partof)]
  return local:one_level($p)


Due to the importance of XML and historically high volumes of data in relational databases , the ISO developed an extension of the SQL standard called SQL / XML in order to combine the possibilities of XML and SQL. XQuery was defined as the query language for querying XML data in the context of the SQL function XMLQuery.


  • BaseX , Open Source: XQuery 3.1, XQuery Full-Text 1.0, XQuery Update 1.0
  • eXist , Open Source: XQuery 3.0, XQuery Update 1.0
  • Saxon , Open Source and Commercial: XQuery 3.1, XQuery Update 1.0
  • Sirix , Open Source: XQuery 1.0 (and XQuery Update 1.0 based on Brackit )
  • xqerl , Open Source: XQuery 3.1, XQuery Update 3.0
  • XQilla , Open Source: XPath 2.0, XQuery Update 1.0
  • Zorba , Open Source: XQuery 3.0, XQuery Full Text 1.0, XQuery Update 1.0;
  • XQuery Test Suite Results : XQuery 1.0 Compliance


Web links

Wiktionary: XQuery  - explanations of meanings, word origins, synonyms, translations

Individual evidence

  1. Michael Wagner: SQL / XML: 2006 - Evaluation of the standard conformity of selected database systems . Diplomica Verlag, 2010, ISBN 3-8366-9609-6 .