Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XQuery Examples

XQuery Examples


In this section, let us learn some basic XQuery syntax by studying an example.


XML example document

We will use this XML document in the examples below.

"books.xml":

  Everyday Italian

  Giada De Laurentiis

  2005

  30.00

  Harry Potter

  J K. Rowling

  2005

  29.99

  XQuery Kick Start

  James McGovern

  Per Bothner

  Kurt Cagle

  James Linn

  Vaidyanathan Nagarajan

  2003

  49.99

  Learning XML

  Erik T. Ray

  2003

  39.95

View the "books.xml" file in your browser.


How to select nodes from "books.xml"?

function

XQuery uses functions to extract data from XML documents.

doc() is used to open the "books.xml" file:

doc("books.xml")

Path expression

XQuery uses path expressions to navigate through elements in an XML document.

The following path expression is used to select all title elements in the "books.xml" file:

doc("books.xml")

/bookstore/book/title

(/bookstore selects the bookstore element, /book selects all book elements under bookstore, and /title selects all title elements under each book element)

The above XQuery can extract the following data:

Everyday Italian Harry Potter XQuery Kick Start Learning XML

predicate

XQuery uses predicates to qualify the data extracted from an XML document.

The predicate below is used to select all book elements under the bookstore element, and the value of the price element under the selected book element must be less than 30:

doc("books.xml")/bookstore/book

[price<30]

The above XQuery can extract the following data:

  Harry Potter

  J K. Rowling

  2005

  29.99