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.
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.
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=
for $i in (1 to 1000) where $i lt 4 return $i
–
Dorpat Though the question is quite old, I am answering it as some new people might face such situation and would get a better solution.
This solution would run easily on BaseX 7.6
for $i in (1 to 10)
return
if ($i = 3) then
$i
else
exit
Output will be - 3
OR this will generate the output - 3,
for $i in (1 to 10)
return
if ($i = 3) then
$i
else
()
$i = 1
it would already exit. –
Rapparee © 2022 - 2024 — McMap. All rights reserved.