Preamble

Lasaris

XQuery

  • Query language for searching and extraction of XML nodes (elements, attributes) from a document and for an output XML document construction.
  • Created by a different W3C group (community) than XSLT
  • Purpose might be the same but XQuery tends to be used for more structured data while XSLT rather for documents (which are more narrative, less structured).
  • SQL-like

XQuery features

  • All XQuery expressions operate on sequences, and evaluate to sequences
  • List of Nodes or atomic values
  • Extending XPath, adding complex queries
  • Extension for updates

Specifications

  • The XQuery is the most common XML query language at present time (and it seems to be the same in the future as well).
  • Based on XPath 2.0 data model (XQuery 1.0) or common XML Schema and XPath 3.0 data model, operators and functions in case of XQuery 3.0.
  • Supported by main database engines producers (IBM, MS, Oracle, etc)

XQuery Specification

Processing of Queries

  • Native XML databases, such as BaseX — used for "real" querying within collections of XML document
  • XML-enabled databases
  • Some XSLT/XQuery processors, such as Saxon — usually for querying just one document

Where XQuery and where XSLT?

The XQuery domain is useful for:

  • queries, where extraction (selection) part is more complicated than the construction part.

In other cases:

  • Use XSLT for more "narrative", less structured inputs and if more complex output is required (e.g. added some new markup and content).
  • Otherwise use a more general API (using DOM manipulation) if more complex operations are required.

Source code example

Example of source document, XML Queries on it and their results.

 version="1.0" encoding="UTF-8"?>
  
     category="friends">
      Petr
      Novak
      1969-05-14
      novak@myfriends.com
       lang="en">Very good friend
    
     category="friends">
      Jaroslav
      Nováček
      1968-06-14
      novacek@myfriends.com
       lang="en">Another good friend
    
     category="staff">
      Jan
      Horak
      1970-02-0
      horak@mycompany.com
       lang="en">Just colleague
    
     category="friends">
      Erich
      Polak
      1980-02-28
      erich@myfriends.com
       lang="en">Good friend
    
 

Example - Simple Query (XPath)

  • Task: "extract all surnames in the addressbook".
  • Query is more-or-less just an XPath expression, like "selects all lastname elements":
    doc('myaddresses.xml')/addressbook/person/lastname

Result

The query to above mentioned document will output:

Novák Nováček
Horák Polák

Complex XQuery (FLWOR)

FLWOR is an acronym of an XQuery structure. It roughly corresponds to the SQL query structure:

(F)or

Initial query part that specifies query cycle including control variable. Results of XPath expression behind the keyword " in" are assigned to the variable.

(L)et

You can assign values of next variable that can be used later in this section.

(W)here

specifies selection condition ie. which nodes (values) selected by for section will be used. The condition can utilize the variables defined in the "let" section.

(O)rder

Defines how the nodes should be ordered.

(R)eturn

Defines what is returned, constructed from extracted nodes (values).

FLWOR — example

  • Condition used to select requested nodes can be specified either in an XPath expression in "for" clause or in the "where" clause.
    "Return Mr. Polak’s birth-date."
     
for $person in doc('myaddresses.xml')/addressbook/person where $person/lastname='Polák'
return $person/date-of-birth
XQuery returns
 
1980-02-28

Output formatting

  • Return clause may contain XML tags, creating new output XML document
  • Enclosed expression to evaluate in { }
for $person in doc('myaddresses.xml')/addressbook/person where $person/lastname='Polák'
return {$person/date-of-birth/text()}
XQuery returns
 
1980-02-28

Nested queries

  • Queries may be nested in each other, use { }
for $person in doc('myaddresses.xml')/addressbook/person where $person/lastname='Polák'
return 
{for $char in $person/characteristics return {$char/text()}}

XQuery returns
 

   
      Good friend
   

XQuery 3

  • group by
  • less strict order of elements
  • function items (lambda functions)
  • let $f := function($x, $y) { $x + $y } return $f(17, 25)
  • try/catch
  • switch
  • ...

XQuery Update

  • XQuery Update Facility 1.0, Recommendation 17 March 2011
  • Extension to provide update features
  • Insert nodes
  • Delete nodes
  • Replace value/node

XQuery Update example

update insert new@mail.com into
for $person in doc('myaddresses.xml')/addressbook/person where $person/lastname='Polák'
update delete doc('myaddresses.xml')/addressbook/person/lastname

Resource on XQuery