开发者

Does XQuery have exit statement for FLWOR expressions

开发者 https://www.devze.com 2023-01-04 16:46 出处:网络
I would like to k开发者_如何转开发now if xquery FLWOR expression has an exit statement like continue and break?

I would like to k开发者_如何转开发now 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=


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 
       ()
0

精彩评论

暂无评论...
验证码 换一张
取 消