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/name
Of 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:
This clause sets up an iteration through the
productelements returned by the path expression. The variable
$prodis bound, in turn, to each
productin the sequence. The rest of the FLWOR is evaluated once for each
product, in this case, four times.
This clause binds the
$prodDeptvariable to the value of the
This clause selects elements whose
deptattribute is equal to
This clause returns the
namechild of each of the three
productelements that pass the