untitled

(ff) #1

8.2 Querying XML Using XQuery 181


will return all of the interview records in the health study database.
Alternatively, one can specify a collection of XML documents for which
one will perform a series of queries. Such a collection is called the “database”
orcorpus. The specification of the corpus will vary from one XQuery engine
to another. Once the corpus is ready, the queries do not have to mention any
documents.
So far we have only considered XPath expressions. Queries can be far
more elaborate. A general XQuery query may have four kinds of clause, as
follows:



  1. forclause. This specifies a loop or iteration over a collection. It says that
    a variable is to take on each value in the collection, one value at a time.
    For example,


for $bmi in
document("healthstudy.xml")//Interview/@BMI

will set the$bmivariable to eachBMIattribute. All variables in XQuery
start with a dollar sign. This clause corresponds to the FROM clause in
SQL queries, except that in SQL one has only one FROM clause, while an
XQuery expression can have any number offorclauses. Most program-
ming languages (including Perl, C, C++, and Java) use “for” to indicate
an iteration process, and the meaning is the same.


  1. whereclause. This restricts which values are to be included in the result
    of the query. This clause corresponds to thewhereclause in SQL queries.
    For example, if one were only interested inBMIvalues larger than 30, then
    the query would be


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

Programming languages like Perl prefer to use the wordifto indicate a
restriction.


  1. returnclause. A query can have any number of variables. The ones
    that should be printed are specified in thereturnclause. All of the
    queries given so far are actually incomplete, since they did not indicate
    what should be printed. To print theBMIvalues, the query above would
    look like this:

Free download pdf