XQuery FLWOR Expressions

XML voorbeelddocument

We zullen in de volgende voorbeelden verdergaan met dit "books.xml"-document (hetzelfde XML-bestand als in het vorige hoofdstuk).

Bekijk het bestand "books.xml" in uw browser.

Als je FLWOR gebruikt om knopen van "books.xml" te selecteren

Bekijk hieronder deze padexpressie:

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 following FLWOR expression selects the same data 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 where the price element value is greater than 30.

order by The statement defines the sorting order. The sorting is 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>