I would like to know if xquery FLWOR expression has an exit statement like continue and break?
For example I want to exit the for loop when a particular condition is reach.
XQuery has an expression called a FLWOR expression, which is similar to a SQL Select statement that that has From and Where clauses. FLWOR is pronounced "flower", and is an acronym for the keywords used to introduce each clause (for, let, where, order by, and return).
The XQuery <tt>for</tt> is not a procedural loop, it's a sequence iterator. That means you cannot "exit" the loop, you just iterate through the sequence until the last item is reached. The if condition will return true if at least one item satisfies the condition.
FLWOR expressions have five clauses: for, let, where, order by, return.
Analogous to the SELECT-FROM-WHERE block in a SQL query, the where clause of XQuery enables the filtering of the data streams. If the expression of a where clause is evaluated with the current variable allocation to true, the execution of the return clause is initiated.
I would like to know if xquery FLWOR expression has an exit statement like continue and break?
For example I want to exit the for loop when a particular condition is reach.
XQuery is a functional language, which among many other things means that there is no strict concept of order of execution. Therefore any attempts to do something specific when something happens, are not meaningful.
The correct approach is to do something if a specific condition is satisfied.
There is no way to exit a FLWOR expression, other than using the error()
function, but this terminates processing.
One shouldn't worry too much about optimization -- many processors have good optimizers.
Thus many processors will evaluate lazily and will stop the evaluation of the FLOWR expression below, the first time it produces result that satisfies the specific-condition()
:
(someFlowerExpression )[specific-condition(.)][1]
XQuery Scripting has an exit statement:
variable $i := 0;
while(true())
{
$i := $i + 1;
if($i = 3) then
exit returning $i
else();
}
Or
for $i in (1 to 1000)
return
if($i = 3) then
exit returning $i;
else();
You can try this example live at http://www.zorba-xquery.com/html/demo#JvSLsVh3ZjhvTHecVd9jyE1vEBc=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With