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":
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:
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: