In this section, let's study an example to learn some basic XQuery syntax. We will apply this XML document in the following example. Reference content: books. xml :? Xmlversion = 1.0 encoding = ISO-8859-1? B
In this section, let's study an example to learn some basic XQuery syntax.
XML instance document
We will apply this XML document in the following example.
Reference content is as follows:
"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
Vaidyan#nagarajan
2003
49.99
Learning XML
Erik T. Ray
2003
39.95
How to select a node from "books. xml?
Function
The XQuery application function is used to extract data from XML documents.
Doc () is used to open the "books. xml" file:
Doc ("books. xml ")
Path expression
The XQuery application path expression is used to navigate through elements in the XML document.
The following path expression is used to select all the title elements in the "books. xml" file:
Doc ("books. xml")/bookstore/book/title
(/Bookstore selects the bookstore element,/book selects all the book elements under the bookstore element, and/title selects all the title elements under each book element)
The preceding XQuery can extract the following data:
Reference content is as follows:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Predicates
XQuery uses predicates to limit the data extracted from XML documents.
The following predicates are used to select all the 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 preceding XQuery can extract the following data:
Reference content is as follows:
Harry Potter
J k. Rowling
2005
29.99