XQuery Example
In this section, let's learn some basic XQuery syntax by studying an example.
XML Example Document
We will use this XML document in the following examples.
"books.xml":
View the "books.xml" file in your browser.
How to Select Nodes from "books.xml"?
Functions
XQuery uses functions to extract data from XML documents.
The doc()
function is used to open the "books.xml" file:
Path Expressions
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: /bookstore/book/title
(/bookstore selects the bookstore element, /book selects all book elements under the bookstore element, and /title selects all title elements under each book element)
The above XQuery can extract the following data:
Predicates
XQuery uses predicates to limit the data extracted from an XML document.
The following predicate is used to select all book elements under the bookstore element where the value of the price element is less than 30: [price<30]
The above XQuery can extract the following data: