XQuery/XPath examples

MotivationEdit

You would like to select specific structures within an XML document. You would like to use a language that is consistent across all W3X XML Standards. This language is XPath.

Sample Input FileEdit

Put this file in /db/apps/training/data/books.xml

<books>
    <description>A list of books useful for people first learning how to build web XML web applications.</description>
    <book>
        <title>XQuery</title>
        <author>Priscilla Walmsley</author>
        <description>This book is a highly detailed, through and complete tour of the W3C Query language.  It covers all the
        key aspects of the language as well as</description>
        <format>Trade press</format>
        <license>Commercial</license>
        <list-price>49.95</list-price>
    </book>
    <book>
        <title>XQuery Examples</title>
        <author>Chris Wallace</author>
        <author>Dan McCreary</author>
        <description>This book provides a variety of XQuery example programs and is designed to work with the eXist open-source native XML application server.</description>
        <format>Wiki-books</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
    <book>
        <title>XForms Tutorial and Cookbook</title>
        <author>Dan McCreary</author>
        <description>This book is an excellent guide for anyone that is just beginning to learn the XForms standard.  The book
        is focused on providing the reader with simple, but complete examples of how to create XForms web applications.</description>
        <format>Wikibook</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
    <book>
        <title>XRX: XForms, Rest and XQuery</title>
        <author>Dan McCreary</author>
        <description>This book is an overview of the key architectural and design patters.</description>
        <format>Wikibook</format>
        <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
        <list-price>29.95</list-price>
    </book>
</books>

XPath provides a number of functions and axes to move around an XML structure.

Sample Test XQuery ScriptEdit

Here is a sample XQuery "driver" for these tests.  To use it just replace the function after the '''return''' on the last line.
xquery version "1.0";
let $books := doc('/db/apps/training/data/books.xml')
 
return count($books//book)

Screen Image of XQuery Results in oXygenEdit

There are several ways to test your XPath expressions. On of the most useful is to put you XML test data in document within eXist and then use a tool such as oXygen to execute the test on the server but then display the results in the oXygen results window. This can be done within oXygen by setting up eXist (not the default internal Saxon) as your "transformation scenario".

The following is a screen image of how these results look when viewed with the oXygen IDE:

screen image of XQuery execution in oXygen IDE

Sample XPath ExpressionsEdit

Return the entire data file

  $books

Get just books and all the data for each book.

  $books//book

Get all the book titles

  $books//title

Get the collection description

  $books/description/text()

Get the descriptions for all the books

  $books//book/description/text()

Counting and MathEdit

Count the number of books using a absolute path

  count($books/book)  (: should return 4 :)

Count the number of books using //. With eXist executes much faster on larger collections.

  count($books//book) (: should return 4 :)

Get a sequence of all the titles in the book collection.

  $books//title/text()

Calculate the total and average price of all the books in the collection.

  sum($books//list-price/text())  (: Should return a number such as 139.84 :)
  avg($books//list-price/text())  (: Should return a number such as 34.96 :)
  min($books//list-price/text())  (: Should return a number such as 29.95 :)
  max($books//list-price1/text())  (: Should return a number such as 44.99 :)

The following scripts show some of these functions and axes in use.

Adding PredicatesEdit

A predicate is a qualifier that is added to the end of an XPath expression. It is usually used to filter out nodes from result set. Predicates are similar to WHERE constructs in SQL.

  $books//book[format='Wikibook']

Get just the titles of the wikibooks

  $books//book[format='Wikibook']/title/text()

Get all the books that contain the word: 'XQuery" somewhere in the title

  $books//book[contains(title, 'XQuery')]/title/text()

Complex PredicatesEdit

  1. node()
  2. text()
  3. *
  4. string(..)
  5. data(..)
  6. child::
  7. parent::
  8. following-sibling::
  9. preceding-sibling::
  10. descendant::
  11. descendant-or-self

Navigating around a tree with distinct tags

Navigating around a tree with a single tag

Last modified on 29 December 2013, at 05:20