XQuery FLWOR Expression
- Previous Page XQuery Example
- Next Page XQuery HTML
XML instance document
We will continue to use this "books.xml" document in the following examples (the same as the XML file in the previous section).
If you use FLWOR to select nodes from "books.xml"
Please see the following path expression below:
doc("books.xml")/bookstore/book[price>30]/title
The above expression selects all title elements under the book elements under the bookstore element, and the value of the price element must be greater than 30.
The following FLWOR expression selects the same data as the above path expression:
for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title
The result is:
<title lang="en">XQuery Kick Start</title> <title lang="en">Learning XML</title>
Through FLWOR, you can sort the results:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
FLWOR is the acronym for 'For, Let, Where, Order by, Return'.
for The statement extracts all book elements under the bookstore element into a variable named $x.
where The statement selects book elements with a price element value greater than 30.
order by The statement defines the sorting order. The sorting is based on the title element.
return The statement specifies what content is returned. In this case, it is the title element.
The result of the above XQuery expression:
<title lang="en">Learning XML</title> <title lang="en">XQuery Kick Start</title>
- Previous Page XQuery Example
- Next Page XQuery HTML