XQuery FLWORExpressions


XML Example Document

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

。


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

Please look at the following path expression:

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

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

The following FLWOR expression selects the same data as the path expression above:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title

Output result:

XQuery Kick Start
Learning XML

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 an acronym for "For, Let, Where, Order by, Return".

forThe statement extracts all book elements under the bookstore element into a variable named $x.

whereThe statement selects book elements whose price element value is greater than 30.

order byThe statement defines the sort order. It will sort according to the title element.

returnThe statement specifies what content to return. Here, it returns the title element.

The result of the XQuery expression above:

Learning XML
XQuery Kick Start
Other Extensions