Note 1: You can add element constructors, if you want to create
a new element to the output. For instance, in (2) below,
an original 'city' element is replaced by a new 'homecity'
element. The expression inside returns the text content of
the 'city' element. Compare this to $a/name which adds
an original 'name' element to the output.
Note 2: if the query is to be included in an XML document,
the '<' characters have to be replaced by character
entity references <
------------------------------------------------------------------------
1
{
for $a in document("studAddr.xml")/persons/person
where $a/birthdate < 1983
return $a/name
}
Result:
John Punin
John Smith
George Lucas
2
{
for $p in document("students.xml")/course/student,
$a in document("studAddr.xml")/persons/person
where $p/@id = $a/@id
return
{
$a/name
{$a/address/city/text()}
$p/project
}
}
Result:
John Smith
Buffalo
80
George Lucas
Buffalo
100
Elizabeth Roberts
Annapolis
50
3
{
for $a in distinct-values(document("studAddr.xml")//city)
return $a
for $b in document("studAddr.xml")/persons/person
for $c in document("students.xml")//student
where value-equals($b/address/city,$a) and
$b/name = $c/name
return {$b/name/text()}
}
Result:
Annapolis
Elizabeth Roberts
Buffalo
John Smith
George Lucas