XQuery FLWOR Expression

XML Sample Document

We will continue to use this "books.xml" document in the following examples (the same as the XML file in the previous section).

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

If you use FLWOR to select nodes from "books.xml"

Please see the following path expression:

doc("books.xml")/bookstore/book[price>30]/title

The above expression can select all title elements under the book elements under the bookstore element, and the value of the price element must be greater than 30.

The data selected by the following FLWOR expression is the same 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>

With 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. It will sort based on the title element.

return The statement specifies what content is returned. In this case, it returns the title element.

The result of the above XQuery expression is:

<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>