FLWOR
Encyclopedia
The programming language XQuery
defines FLWOR as an expression that supports iteration and binding of variables to intermediate results. FLWOR is an acronym: FOR
, LET
, WHERE, ORDER BY, RETURN. FLWOR is loosely analogous to SQL
's SELECT-FROM-WHERE
and can be used to provide join-like functionality to XML
documents.
First column of the XQuery request shows the for, let, where, order by and return keywords of the FLWOR paradigm. In plain English, this could be read as "Get all departments that have more than ten employees, order these departments by decreasing average salary, and return a report of department numbers, head counts and average salary in each big department". The result could look like:
XQuery
- Features :XQuery provides the means to extract and manipulate data from XML documents or any data source that can be viewed as XML, such as relational databases or office documents....
defines FLWOR as an expression that supports iteration and binding of variables to intermediate results. FLWOR is an acronym: FOR
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....
, LET
Assignment (computer science)
In computer programming, an assignment statement sets or re-sets the value stored in the storage location denoted by a variable name. In most imperative computer programming languages, assignment statements are one of the basic statements...
, WHERE, ORDER BY, RETURN. FLWOR is loosely analogous to SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
's SELECT-FROM-WHERE
Select (SQL)
The SQL SELECT statement returns a result set of records from one or more tables.A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used Data Manipulation Language command...
and can be used to provide join-like functionality to XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
documents.
- for creates a sequence of nodes
- let binds a sequence to a variable
- where filters the nodes on a boolean expression
- order by sorts the nodes
- return gets evaluated once for every node
Example
for $d in doc("depts.xml")//deptno
let $e := doc("emps.xml")//employee[deptno = $d]
where count($e) >= 10
order by avg($e/salary) descending
return
{ $d,
{count($e)} ,
{avg($e/salary)}
}
First column of the XQuery request shows the for, let, where, order by and return keywords of the FLWOR paradigm. In plain English, this could be read as "Get all departments that have more than ten employees, order these departments by decreasing average salary, and return a report of department numbers, head counts and average salary in each big department". The result could look like:
17
25
12500
24
18
11327
3
32
10725
Example using MS SQL Server
DECLARE @xml XML
SET @xml =
'<root_element>
<branch_element>
<item_1>42</item_1>
<item_2>27</item_2>
</branch_element>
<branch_element>
<item_1>a</item_1>
<item_2>b</item_2>
</branch_element>
</root_element>'
SELECT
x.y.query('for $s in self::node return $s//item_1/text') as i,
x.y.query('for $s in self::node return $s//item_2/text') as j
FROM @xml.nodes('/root_element') AS x(y);