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 3

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




Application Security Testing: An Integral Part of DevOps

Variable Binding and Scoping
Although for and let both bind variables, the manner in which they do so is quite different. A let clause binds variables directly to the expression as a whole. However, the for loop binds variables to each value of the sequence returned by the expression. Consider the following query:

# see XQuery33.ixq in samples.zip let $c := document("data/customers.xml")//customer/custno return <customers> {$c} </customers>

The query returns the custno elements for each customer in the customers.xml file, placing them between the the explicit tags. In other words, because the expression binds the variable $c to the sequence of custno elements in customers.xml. The let clause generates only one tuple for this binding. Therefore, the query engine must invoke the return clause only once, generating the following output:

<customers> <custno>9000</custno> <custno>1001</custno> <custno>1003</custno> <custno>2005</custno> </customers>

Now look at the query rewritten to use a for loop instead:

# see XQuery34.ixq in samples.zip for $c in document("data/customers.xml")//customer/custno return <customers> {$c} </customers>

In this case, the query binds the $c variable to the first mathing element (9000, then the next, 1001, etc. Therefore the query engine invokes the return clause four times, generating the following result:

<customers> <custno>9000</custno> </customers> <customers> <custno>1001</custno> </customers> <customers> <custno>1003</custno> </customers> <customers> <custno>2005</custno> </customers>

You must bind a variable before using it. After binding a variable bound in a for or let clause, the variable remains in scope until the end of the FLWR expression in which it is bound. Localized bindings take priority over less localized bindings. In other words, if a variable name used in a binding is already bound, binding the variable again assigns the variable to the newly bound value until the variable goes out of scope. At that point, the variable again refers to the prior binding. Here's an example:

# see XQuery35.ixq in samples.zip for $p in document("data/PO.xml")//po return <new_orders> { for $j in $p//item for $p in document("data/items.xml")//item where $p/itemno=$j/itemno return <item>{$p/description/text()}</item> } </new_orders>

The preceding query lists all item descriptions in PO.xml. The outermost for loop binds $p binds to each po element in the document PO.xml. However, in the inner most for clause, $p is used again to bind to each item element in items.xml. After the inner loop completes, $p$ goes out of scope and regains its initial value from the outer for loop binding (to po in PO.xml again.

The query result looks like this:

<new_orders> <item> Scooter </item> <item> Digital Camera </item> </new_orders> <new_orders> <item> Ping Pong ball </item> <item> Fresh Roses </item> </new_orders>

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