Does XQuery have exit statement for FLWOR expressions
Asked Answered
S

3

6

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.

Schenck answered 26/6, 2010 at 15:19 Comment(1)
FLWOR expressions aren't really loops -- there's no guarantee that "for" elements will be evaluated in a given order, or even in a single thread; they could all be done in parallel and combined together at once, and as long as the semantics were maintained this would be valid.Faris
P
7

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]
Peshawar answered 27/6, 2010 at 18:17 Comment(0)
C
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=

Crutch answered 12/5, 2012 at 14:16 Comment(3)
There is no exit or returning keywords in XQuery.Dorpat
for $i in (1 to 1000) where $i lt 4 return $i Dorpat
Both links are dead :(Rapparee
C
0

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 
       ()
Caitiff answered 20/11, 2013 at 7:48 Comment(3)
I didn't have time to try this in BaseX, but theoretically this would output nothing, since first time through the loop, because of $i = 1 it would already exit.Rapparee
@Vincent - I have updated the answer. The second one does work with BaseX, I have tested. And aslo, the output will be ONLY 3.Caitiff
No, but when I tested this, output is 3 but even after 3, it keeps looping. What the question seeks is something like once i reaches 3, exit on 4Affecting

© 2022 - 2024 — McMap. All rights reserved.