XQuery is the XML query language and refers to a specified by the W3C query language for XML databases.

XQuery uses a ajar to XSLT, SQL, and C syntax and uses XPath and XML schema for its data model and its function library. XQuery has emerged from the languages ​​XQL, XML -QL and Quilt.

XQuery is strongly typed and Turing -complete.

Was significantly involved in the development ( as with quilt ) Donald D. Chamberlin.

Language elements

Except Path (XPath) expressions, there are a number of other language features, which are explained in the following sections based on short examples.

Data model

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

The following sequences are identical:

  • (1, 2, 1 ) and (1, (2, 1))
  • ( 1 () ) and ( 1 )
  • ( ) And

XQuery provides six functions to query cardinalities on sequences available:

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

Variables are referred to in XQuery with $ prefix.

Editing sequences

  • The comma operator is behind each two sequences
  • Add values ​​you can with the function fn: insert -before
  • Can you delete values ​​with the function fn: remove
  • Values ​​may order a new one with the function fn: reverse
  • Can rearrange values ​​to the function fn: unordered

Construction of XML elements

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

    { $ page / head / content ()}     { $ page / body / content ()}   " Computed constructors "

Html element {     element head { $ page / head / content ()}     element body {          bgcolor attribute { $ style / bgcolor / text ( ) },          text { $ page / body / content ()}     }   } For the construction of XML data are both direct notation as XML and the more declarative structure by means of the so-called "computed constructors " are available. In both cases, the braces { ... } are used to embed any other XQuery expressions in the constructor.

Sorting functionality ( "order by " )

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

FLWOR expressions

A central role in XQuery play the so-called FLWOR expressions (pronounced flower ). FLWOR is an acronym for the constructs for, let, where, order by and return, and can be considered analogous to the (SELECT, FROM, WHERE ) - are considered constructs in SQL. Unlike SQL FLWOR expressions, however, are case-sensitive. FLWOR expressions form sequences to sequences (cf. data model). FLWOR expressions have the following generic form:

For $ forvar1 at $ posvar1 in , $ forvar2 at $ posvar2 in ... let $ letvar1: = , $ letvar2: = ... where order by ascending / descending return Here for any other XQuery expressions, and is an expression of type boolean. The for - constructs bind their variables $ forvar1, $ forvar2, ... sequentially to each a value from the binding sequences . The position of the preceding variable in the sequence can be bound to a position variable by using the keyword at, where the numbering starts at 1. In contrast, the let- constructs bind their variables $ letvar1, $ letvar2, ... to the entire result of the associated expression. The resulting Tupelfolge includes all possible combinations of variable bindings that can be formed from the assignment of the for and let variable.

The where construct is used to eliminate unwanted tuples. It makes sense to take his Boolean expression with respect to at least one of the bound in the for- let- constructs and variables. The same applies to the order by construct that is used to sort the tuples. In return construct finally the variables of interest to be returned, possibly embedded in directly or indirectly constructed elements. In more complex cases FLWOR expression also be nested, meaning that at the location of each occurrence of in turn can be any FLWOR expression.

The following example illustrates the use of for and let. The for- part of the expression selects all paragraphs within an HTML page (

elements). Let- in part, the number of words is determined for each paragraph with the aid of predefined functions. Then all non-empty paragraphs ( word count > 0) can be expanded to 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

           {$ par / content ()}
           Size: { $ words }         It should be noted in this expression that for- and let- part are interdependent, which declared in the for- part variable $ par is used in the let- part. For the analysis, this means that the let part must be par reevaluated for each assignment of the variable $.

Composite operations and groupings

Composite operations ( or English. Joins) denote a set operation consisting of the formation of a Cartesian product of two input quantities and subsequent selection (see Relational Algebra ). In the example of the previous section to FLWOR expressions such composite operation is already shown. The bindings declared in the for- and let- variable parts are the respective input quantities on the Cartesian product of the selection conditions of the where part to be applied. Since selection conditions both in the where part of the expression as well as within the for and let- parts can occur in Subausdrücken, a normalization is necessary before the evaluation of an expression.

Predefined functions

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

User-Defined Functions

Using declare function can create custom functions are declared. The general form is as follows:

Declare function namespace: function-name   ($ parameter1 as datentyp1, $ parameter2 as datentyp2, ...) as returnable data_type {         } Functions, other functions and call itself recursively, so that XQuery is Turing -complete.

Different logical operators

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

  • Eq: Tests for equality ( equal)
  • Ne: Tests for inequality ( not equal )
  • Lt: Checks for Small (less than )
  • Gt: Checks for Greater ( Greater than )
  • Le: Checks for Less than or equal (Less or equal)
  • Ge: Checks for Greater than or equal (Greater or equal)

The "usual" comparison operators ( =,! =, < ,>, < =,> = ) Through an existential comparison. It can left and right of the operator sequences are. Thus, the expression seq2 seq1 OP is only true if an element e1 of seq1 and seq2 exist with a e2 e1 e2 OP, where OP one of the operators =,! =, < ,>, < = Or> =. Thus (1, 2 )> ( 3, 0 ) is true, since 1> 0.

XQuery is intended to identify, from large XML data collections individual parts. In contrast, XSLT is used to transform a complete XML documents.


Count all of the question - XML elements in the source document:

Fn: count ( / / question ) Complex example, listing all questions of all documents of a gallery for KEduca who have less than two answers:

     {      for $ s in ( fn: doc ( " lpi101.edugallery ") / / server / @ address)          for $ d in (fn: doc ( $ s) [/ / question [( count ( true) count ( false)) < = 1 ]])              return {                      for $ q in ($ d / / question [( count ( true) count ( false)) < = 1] )                          return $ q                  } < / doc >      } Another example (which shows the possibility of indirect restructuring of XQuery)

  • Source document:

                              < / part list> Target document:

                                                                  Solution in XQuery:

Declare function local: One_level ( $ p as node () ) as node ()   {         {       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)   }   application

Because of the importance of XML and historically high data sets in a relational database has been developed by an extension of the SQL Standards ISO named SQL / XML to combine the capabilities of XML, and SQL. As a query language for querying XML data in the context of the SQL function XMLQuery XQuery is defined.


  • BaseX, open source, supports XQuery Full-Text and XQuery Update; Live Demo
  • EXist, open source, supports XQuery Update, extensive libraries for developing web applications
  • Saxon
  • Sirix, open source, supports XQuery ( XQuery Update Facility and on the basis of Brackit ) and major expansions for the effective and efficient versioning
  • XQilla, open source ( Apache License v2), XPath 2.0, XQuery Update, XQuery Full-Text, partial XSLT
  • Zorba, from the FLWOR Foundation; Live Demo