FLWOR Expressions
FLWOR expressions, also known simply as FLWORs, are used for queries that are more complex. In addition to allowing more readable and structured selections, they allow functionality such as joining data from multiple sources, constructing new elements and attributes, evaluating functions on intermediate values, and sorting results.
FLWOR (pronounced "flower"), stands for "for, let, where, order by, return," the keywords that are used in the expression. Example 6-1 shows a FLWOR that is equivalent to the second path expression from the previous section.
Example 6-1. FLWOR
for $prod in doc("catalog.xml")//product
let $prodDept := $prod/@dept
where $prodDept = "ACC" or $prodDept = "WMN"
return $prod/nameOf course, this is far more verbose, and for such a simple example, the path expression is preferable. However, this example is useful as an illustration before moving on to examples that are more complex. As you can see, the FLWOR is made up of several parts:
-
for This clause sets up an iteration through the
productelements returned by the path expression. The variable$prodis bound, in turn, to eachproductin the sequence. The rest of the FLWOR is evaluated once for eachproduct, in this case, four times.-
let This clause binds the
$prodDeptvariable to the value of thedeptattribute.-
where This clause selects elements whose
deptattribute is equal toACCorWMN.-
return This clause returns the
namechild of each of the threeproductelements that pass thewhere ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access