untitled

(ff) #1

182 8 Query Languages


for $bmi in
document("healthstudy.xml")//Interview/@BMI
where $bmi > 30
return $bmi

Thereturnclause corresponds to the SELECT clause in an SQL query.
Programming languages like Perl also use the word “return,” but the
meaning is completely different.


  1. letclause. It is often convenient to do computations in a query. To sim-
    plify the computations, it is helpful to be able to introduce variables that
    are set to expressions using the other variables. This is done usinglet
    clauses. There is no analogous capability in SQL. For example, the follow-
    ing query has the same result as the one above:


let $bmilist :=
document("healthstudy.xml")//Interview/@BMI
for $bmi in $bmilist
where $bmi > 30
return $bmi

The$bmilistvariable is set to the whole collection ofBMIvalues. The
forclause then sets$bmito each of the values in this collection, one at a
time.

XQuery uses variables in ways that are different from how they are used
in programming languages such as Perl. In Perl, the dollar sign is used to
indicate that a variable is a scalar. A different symbol, the at-sign (@), is used
to indicate variables that can have an array of values. In XQuery, there is no
distinction between scalars and arrays: a variable can be either one. More
significantly, Perl variables can be assigned to a value any number of times.
XQuery will only assign a variable to different values in aforclause. A
variable can only be given a value once by aletclause. Subsequentlets
for the same variable are not allowed.
One can build up more complicated XQuery expressions by combining a
series offorandletclauses. These are followed by an optionalwhere
clause. Thereturnclause is always the last clause. For example, suppose
that one wants to obtain the major topics of the Medline articles in a corpus
of Medline citations. One would use a query like this:
Free download pdf