Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XQuery FLWOR Expression

XQuery FLWOR Expression


XML example document

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

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


If using FLWOR to select nodes from "books.xml"

Please look at the path expression below:

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

This expression above 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 data selected by the FLWOR expression below is the same as that selected by 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 that takes the first letters of "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 the book elements whose price element value is greater than 30.

order by The statement defines the sort order. Sorting will be based on the title element.

return The statement specifies what to return. Here, the title element is returned.

The result of the above XQuery expression:

Learning XML XQuery Kick Start