Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Introduction to XQuery (Part 3 of 4) : Page 2

Learn how to use XQuery's FLWR (for, let where, and return) expressions to process and restructure data from one or more documents.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

FLWR Syntax and Semantics
The FLWR expression can be explained with the help of the block diagram shown in Figure 1.

Figure 1: The parts and processing sequence of an XQuery FLWR expression.
A FLWR expression must contain at least one for or let clause. The task of a for or let clause is to evaluate expressions and assign (bind) the results of the expressions to variables. The difference between these two clauses is that a for clause creates "n" different bindings when the expression results in a sequence (see bottom of first page, Part 2 of this series with "n" members, and a let clause creates one single binding (where the variable is bound to the entire sequence resulting from the associated expression). These variable bindings are then passed on to a where clause that filters them based on some conditions. In this respect, the where clause in a FLWR expression is similar to the where clause in a SQL select statement. The return clause constructs the results of the FLWR expression, and is invoked once for every variable binding that survives the filter (where clause).

FLWR expressions are useful for processing and restructuring data from one or more documents.
For example, suppose you wanted to to list all the items in the file items.xml that have been ordered by customers in the PO.xml file. You could do that it using the following query:

# see XQuery32.ixq in samples.zip for $i in document("data/items.xml")//item let $p := document("data/PO.xml")//po where $i/itemno = $p//itemno return <ordered_item> {$i/description/text()} </ordered_item>

The for loop iterates through each item element in items.xml, while the where clause selects those items whose itemno appears in each po element in the file PO.xml, as specified by the let clause. The return clause lists each ordered item's description as an ordered_item element. The result of the query looks like:

<ordered_item>Scooter</ordered_item> <ordered_item>Digital Camera</ordered_item> <ordered_item>Ping Pong ball</ordered_item> <ordered_item>Fresh Roses</ordered_item>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date