Storing and Querying XML data; XQuery

Querying

Characteristics

Characteristics (2nd)

Where to use XQuery (and where not)

The XQuery domain is useful for:

In other cases:

Source code example

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


  
    
      Petr
      Novak
      1969-05-14
      novak@myfriends.com
      Very good friend
    
    
      Jaroslav
      Nováček
      1968-06-14
      novacek@myfriends.com
      Another good friend
    
    
      Jan
      Horak
      1970-02-0
      horak@mycompany.com
      Just colleague
    
    
      Erich
      Polak
      1980-02-28
      erich@myfriends.com
      Good friend
    
 

Example - Simple Query (XPath)

Running XQuery using Saxon 9.0j

XSLT processor Saxon contains the XQuery processor since version 8.x as well. To process XQuery you need:

Result

The query to above mentioned document will create the file result.xml:

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

XQuery structure

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 oredered.

®eturn

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

FLWOR - simple 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:


l980-02-28

XQuery Implementation

SAXON since versions 7.x:

Native XML databases

Native XML database systems mostly support XQuery as a query language: