










































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Bhavesh Verma for Bioinformatics Using Semantic Computing course at Bhagwant University Rajasthan. It includes: Selecting, Joining, FLWOR, Expressions, Queries, Evaluating, Function, Intermediate, Values, Keywords
Typology: Slides
1 / 82
This page cannot be seen from the preview
Don't miss anything!











































































FLWOR expressions, also known simply as FLWORs, are used for queries that are more complex.
In addition to allowing more readable and structured selections, they allow functionality such as joining data from multiple sources, constructing new elements and attributes, evaluating functions on intermediate values, and sorting results.
For:This clause sets up an iteration through the product elements returned by the path expression.
Let:This clause binds the $prodDept variable to the value of the dept attribute.
Where:This clause selects elements whose dept attribute is equal to ACC or WMN.
Return:This clause returns the name child of each of the three product elements that pass the where clause.
It sets up an iteration that allows the rest of the FLWOR to be evaluated multiple times, once for each item in the sequence returned by the expression after the in keyword.
This sequence, also known as the binding sequence, can evaluate to any sequence of zero, one or more items
The FLWOR expression with its for clause is similar to loops in procedural languages such as C. However, one key difference is that in XQuery, because it is a functional language, the iterations are considered to be in no particular order. They do not necessarily occur sequentially, one after the other.
One manifestation of this is that you cannot keep variable counters that are incremented with each iteration, or continuously append to the end of a string variable with each iteration.
Query
for $i in 1 to 3
return
Result
You can use the reverse function if you want to descend in value, as in:
for $i in reverse(1 to 3)
You can also increment by some value other than 1 using an expression like: for $i in (1 to 100)[. mod 2 = 0]
which gives you every other number (2, 4, 6, etc.) up to 100.
Multiple variables can be bound in a single for clause, separated by commas. This has the same effect as using multiple for clauses.
for $i in (1, 2), $j in ("a", "b")
return
This syntax is shorter but can be less clear in the case of complex expressions.
A let clause is a convenient way to bind a variable to a value. Unlike a for clause, a let clause does not result in iteration; it binds the whole sequence to the variable rather than binding each item in turn. The let clause serves as a programmatic convenience that avoids repeating the same expression multiple times. Using some implementations, it can also improve performance, because the expression is evaluated only once instead of each time it is needed.
The FLWOR with the let clause returns only a single oneEval element, because no iteration takes place and the return clause is evaluated only once.
One or more let clauses can be intermingled with one or more for clauses. Each of the let and for clauses may reference a variable bound in any previous clause.
The only requirement is that they all appear before any where, order by, or return clauses of that FLWOR.
let $doc := doc("catalog.xml")
for $prod in $doc//product
let $prodDept := $prod/@dept
let $prodName := $prod/name
where $prodDept = "ACC" or $prodDept = "WMN"
return $prodName
for $prod in doc("catalog.xml")//product
let $prodDept := $prod/@dept
where $prod/number > 100
and starts-with($prod/name, "F")
and exists($prod/colorChoices)
and ($prodDept = "ACC" or $prodDept = "WMN")
return $prod
The return clause consists of the return keyword followed by the single expression that is to be returned.
It is evaluated once for each iteration, assuming the where expression evaluated to true.
The result value of the entire FLWOR is a sequence of items returned by each evaluation of the return clause.
for $i in (1 to 3) return