Jump to content

XQuery/XPath examples

From Wikibooks, open books for an open world

Motivation

[edit | edit source]

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

Sample Input File

[edit | edit source]

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>
    <book>
        <title>Making Sense of NoSQL</title>
        <author>Dan McCreary</author>
        <author>Ann Kelly</author>
        <description>A guide to NoSQL for managers and solution architects.  Presents six NoSQL architectures and when to use each architecture.</description>
        <format>Trade Press</format>
        <license>Commercial</license>
        <list-price>32.00</list-price>
    </book>
</books>

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

Sample Test XQuery Script

[edit | edit source]

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 oXygen

[edit | edit source]

There are several ways to test your XPath expressions. One of the most useful is to put your 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 Expressions

[edit | edit source]

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 Math

[edit | edit source]

Count the number of books using a absolute path

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

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

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

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 171.8 :)
  avg($books//list-price/text())  (: Should return a number such as 34.36 :)
  min($books//list-price/text())  (: Should return a number such as 29.95 :)
  max($books//list-price1/text())  (: Should return a number such as 49.95 :)

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

Adding Predicates

[edit | edit source]

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.

Get all the wikibooks

  $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 Predicates

[edit | edit source]
  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