XQuery Tutoriallogin
XQuery Tutorial
author:php.cn  update time:2022-04-21 16:43:44

XQuery FLWOR expression



XML Example Document

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

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


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 can select all title elements under the book element 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 the path expression above:

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

Output result:

<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". The

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

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

order by statement defines the sort order. Will be sorted based on the title element.

return statement specifies what to return. What is returned here is the title element.

The result of the above XQuery expression:

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

php.cn