Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XQuery FLWOR expression


May 28, 2021 XQuery


Table of contents


XQuery FLWOR expression

FLWOR expressions are one of XQuery's most important expressions, and this section takes you through the use of FLWOR expressions.


XML instance documentation

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

View the "books" file .xml your browser.


If you use FLWOR to select a node .xml "books"

Look at this path expression:

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

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

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

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

Output:

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

The for statement extracts all the book elements under the bookstore element into a $x called the file.

The where statement selects a book element with a price element value greater than 30.

The order by statement defines the sort order. Sorts are based on the title element.

The return statement specifies what is returned. The title element is returned here.

The result of the XQuery expression above:

<title >Learning XML</title>
<title >XQuery Kick Start</title>

Next, in the next section, you'll see how FLWOR and HTML work together in XQuery.