25.12.2013 Views

Tamino XQuery User Guide - Software AG Documentation

Tamino XQuery User Guide - Software AG Documentation

Tamino XQuery User Guide - Software AG Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

FLWOR Expressions<br />

Currently, there are 4 books stored.<br />

In a let clause each variable is bound directly to the result of an expression. A single tuple is then<br />

generated that contains all variable bindings. The return clause is evaluated once for this single<br />

tuple. As $a is bound to all instances of book elements, count($a) evaluates to the total number<br />

of books present in the collection. Since the return clause is invoked only once, we get the expected<br />

result. Now consider the same expression using the for clause:<br />

for $a in input()/bib/book<br />

return<br />

Currently, there are { count($a) } books stored.<br />

The result is:<br />

Currently, there are 1 books stored.<br />

Currently, there are 1 books stored.<br />

Currently, there are 1 books stored.<br />

Currently, there are 1 books stored.<br />

Instead of creating a single tuple, the for clause creates tuples of variable bindings from the<br />

Cartesian product of the sequence of items to which the expressions evaluate. In this example this<br />

means that the variable is bound to the evaluation of the expression input()/bib/book resulting<br />

in a sequence of four document instances. So $a is bound four times. For each of these bindings a<br />

tuple is generated and the return clause is called for each tuple. The count function only sees one<br />

document instance at a time.<br />

To further illustrate the processing of for clauses consider this example that is published in a<br />

similar form in the W3C <strong>XQuery</strong> specification:<br />

for $i in (1, 2),<br />

$j in (3, 4)<br />

return<br />

<br />

{ $i }<br />

{ $j }<br />

<br />

In the example before there is only one expression in the for clause so that the Cartesian product<br />

is equivalent to the sequence of items that the expression evaluates to. In this query you can see<br />

that the tuples of variable bindings are created from the Cartesian product of the sequences (1,<br />

2) and (3, 4):<br />

34<br />

<strong>XQuery</strong> <strong>User</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!