Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does XQuery have exit statement for FLWOR expressions

Tags:

xml

xquery

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.

like image 698
Technocrat Avatar asked Jun 26 '10 15:06

Technocrat


People also ask

What is XQuery explain XQuery FLWOR expression?

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).

How do you exit a loop in XQuery?

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.

What is one of the types of clauses supported by a FLWOR query?

FLWOR expressions have five clauses: for, let, where, order by, return.

Where clause in XQuery?

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.


2 Answers

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]
like image 81
Dimitre Novatchev Avatar answered Oct 16 '22 22:10

Dimitre Novatchev


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=

like image 1
wcandillon Avatar answered Oct 16 '22 23:10

wcandillon