XQuery Basics
This is a method of running queries on XML. They are an extension of XPath and therefore every XPath is an XQuery expression.
For additional XQuery examples see this lecture video
Syntax
For the following tree:
graph
$doc -->|students|a[ ]
a -->|student|$s
$s -->|name| ''Anna''
$s -->|module| ''COMP207''
an XQuery could look like the following:
let $doc := doc("students.xml")
for $s in $doc/students/student
where $s/module = "COMP207"
return $s/name
This is case sensitive.
FLWR Expressions
| Clause | Requirement |
|---|---|
for & let |
Any number, interleaved. Some interpreters require at least one let. |
where |
Optional |
return |
Mandatory |
Therefore we can get the following smallest expressions depending on the implementation:
return <greeting>Hello World!</greeting>
let $hello := <greeting>Hello World!</greeting>
return $hello
Clauses
letfor variable in XQuery expression- Assigns the result of an expression to a variable.
forlet variable := XQuery expression- For each item in the result of the expression assign it the the variable.
wherewhere condition 1, condition 2, ...- If all the conditions are true then execute the return clause.
- You can also use the following syntax:
where some/every $var in XQuery expression satisfies conditionComparisons between expressions
$s1/name = $s2/namemay not have the tags removed.