XML XQuery
XQuery is a functional query language purpose-built for extracting, filtering, and reshaping data stored as XML, playing much the same role for XML that SQL plays for relational tables.
What Is XQuery?
XQuery treats an XML document (or a collection of documents) as a queryable tree and lets you ask questions of it directly: which products cost more than $50, sorted by name? which employees report to a given manager? XQuery is built on top of XPath — every XPath expression is a valid XQuery expression — but it adds the ability to construct new XML output, define reusable functions, and combine data from multiple sources in a single query, which is why it's often described as 'SQL for XML.'
FLWOR Expressions
A Basic FLWOR Query
<!DOCTYPE html>
<html>
<body>
<pre>
for $product in doc("catalog.xml")/catalog/product
where $product/price > 20
order by $product/name
return
<item>
<name>{ $product/name/text() }</name>
<price>{ $product/price/text() }</price>
</item>
</pre>
</body>
</html>FLWOR — pronounced 'flower' — stands for the five clauses that give the expression its shape: for binds a variable to each item in a sequence, let assigns a computed value, where filters the bound items, order by sorts what's left, and return builds the output for every surviving binding. Only for and return are required; let, where, and order by are optional and can be repeated or omitted as the query demands.
Filtering and Joining Two Documents
<!DOCTYPE html>
<html>
<body>
<pre>
for $order in doc("orders.xml")/orders/order
let $cust := doc("customers.xml")/customers/customer[id = $order/customerId]
where $order/total > 100
order by $order/total descending
return
<receipt>
<customer>{ $cust/name/text() }</customer>
<total>{ $order/total/text() }</total>
</receipt>
</pre>
</body>
</html>- for — iterates over a sequence, binding each item in turn to a variable
- let — assigns the result of an expression to a variable without iterating
- where — filters bindings using a boolean condition
- order by — sorts the remaining bindings before output is produced
- return — constructs the XML (or other) output for each surviving binding
- XPath — the path syntax XQuery uses to navigate and select nodes
- Functions — built-in (fn:count, fn:concat, fn:doc) or user-defined with declare function
Writing Functions and Constructing Elements
A Reusable XQuery Function
<!DOCTYPE html>
<html>
<body>
<pre>
declare function local:discounted-price($price as xs:decimal, $percent as xs:decimal) as xs:decimal {
$price - ($price * $percent div 100)
};
for $product in doc("catalog.xml")/catalog/product
return
<product>
<name>{ data($product/name) }</name>
<salePrice>{ local:discounted-price($product/price, 15) }</salePrice>
</product>
</pre>
</body>
</html>Exercise: XML XQuery
What is XQuery primarily designed to do?