Preamble

Lasaris

Outline

  • Storing XML data
  • Querying XML data
  • XQuery

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

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

Storing XQuery Files

  • Usual file extensions for XQueries: .xq, .xqy, .xquery

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.

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

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

Running XQuery using Saxon 9.0j

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

  • to install Saxon 9.0.0.4J for example ("j" means implementation in Java, there is a .NET implementation as well) by unpacking into folder c: /devel/saxon9-0-0-4j for example.
  • Change working directory to the folder: cd c:/devel/saxon9-0-0-4j
  • put the above mentioned query into a file (lastnames.xq).
  • store the above mentioned XML document containing "addressbook" into the file myaddresses.xml in the same directory.
  • Run:
    java -classpath saxon9he.jar net.sf.saxon.Query -o result.xml lastnames.xq

Result

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

<lastname>Novák</lastname> <lastname>Nováček</lastname>
<lastname>Horák</lastname> <lastname>Polák</lastname>

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 — 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
<?xml version=" 1.0" encodings"UTF-8"?>
<date-of-birth>l980-02-28</date-of-birth>

Standalone XQuery Implementations

SAXON since versions 7.x:

  • install (extract) Saxon with version 7.0 at least (8.x, 9.x) into some directory
  • change working directory to the Saxon directory and
  • run: java -classpath saxon9.jar net.sf.saxon.Query -o result.xml query-file .xq from command line.
  • There is a .NET Saxon implementation (means .DLL and .EXE files)

Native XML databases

Native XML database systems mostly support XQuery as a query language: - BaseX:: http://basex.org - Berkeley DB XML:: http: //www.sleepycat.com/products/index.shtml - eXist:: http://exist.sourceforge.net/

Resource on XQuery