FLWOR Expressions

The FLWOR expression is the workhorse of XQuery: It allows a user to iterate over sequences of values, compute intermediate results, and conditionally filter values. Like the typing of path expressions, the typing of FLWOR expressions also relies on factored types.

FLWOR expressions iterate over the items in a sequence. Here is an example in which we suppose that $articles has type element(article)+ :

 for $article in $articles return $article/end_date - $article/start_date 

has type xdt:dayTimeDuration +. To type the body of the for expression, we strip off the occurrence indicator from element(article)+ and type the body of the for expression assuming that $article has type element(article) , yielding type xdt:dayTimeDuration . Then we add back the occurrence indicator, to get the type xdt:dayTimeDuration+ . The same rule applies for other occurrence indicators:

 for $article in $articles[start_date <= current_date()] return $article/end_date - $article/start_date 

has type xdt:dayTimeDuration* , and

 for $article in $articles[start_date <= current_date()][1] return $article/end_date - $article/start_date 

has type xdt:dayTimeDuration? .

In general, the type of the input sequence in the FLWOR expression will be factored; recall that a factored type consists of a choice of items and an occurrence indicator. The body of the for expression is typed assuming that the iteration variable has as its type the choice of item types, and the occurrence indicator will be added to the type of the body to give the type of the FLWOR expression.

The effect of a where clause on the type of a FLWOR expression is similar to the effect of a predicate on the type of a path expression:

 for $article in $articles where $article/start_date <= current_date() return $article/end_date - $article/start_date 

has type xdt:dayTimeDuration* .

A FLWOR expression may also contain an order by clause:

 for $article in $articles order by $article/end_date - $article/start_date return $article 

has type element(article)+ . Because the type of the input sequence in the FLWOR expression is factored, reordering the sequence returned by the FLWOR does not change its type.



XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net