XML XQuery
XQuery is a powerful query language designed specifically for extracting and transforming data from XML documents. While XSLT focuses on transforming the presentation of XML, XQuery is primarily focused on querying XML — much like SQL is used to query relational databases. XQuery is the standard language for retrieving specific information from XML databases and large XML data collections.
XQuery is built on top of XPath, so all XPath expressions are valid within XQuery.
What Can XQuery Do?
- Search XML data and extract specific elements or attributes.
- Filter results based on conditions.
- Sort and group XML data.
- Join data from multiple XML documents.
- Construct new XML structures from existing data.
- Query XML databases like eXist-db, BaseX, or MarkLogic.
The Source XML Document
All examples in this topic use the following XML file, saved as products.xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product id="P01" category="electronics">
<name>Wireless Headphones</name>
<brand>SoundWave</brand>
<price>79.99</price>
<stock>120</stock>
</product>
<product id="P02" category="electronics">
<name>USB-C Hub</name>
<brand>TechLink</brand>
<price>34.50</price>
<stock>85</stock>
</product>
<product id="P03" category="accessories">
<name>Laptop Bag</name>
<brand>CarryPro</brand>
<price>45.00</price>
<stock>60</stock>
</product>
<product id="P04" category="accessories">
<name>Screen Cleaner Kit</name>
<brand>CleanTech</brand>
<price>12.99</price>
<stock>200</stock>
</product>
</catalog>
FLWOR Expressions
The core construct in XQuery is the FLWOR expression. The name is an acronym for the five clauses it supports:
| Clause | Full Name | Purpose |
|---|---|---|
for | For | Iterates over a sequence of nodes |
let | Let | Assigns a value to a variable |
where | Where | Filters results based on a condition |
order by | Order By | Sorts the results |
return | Return | Specifies what to output |
Basic XQuery Examples
Example 1: Select All Product Names
for $product in doc("products.xml")/catalog/product
return $product/name
This loops through every <product> element and returns its <name>. The variable $product holds one product at a time during each iteration.
Output:
<name>Wireless Headphones</name>
<name>USB-C Hub</name>
<name>Laptop Bag</name>
<name>Screen Cleaner Kit</name>
Example 2: Filter Products by Category
for $product in doc("products.xml")/catalog/product
where $product/@category = "electronics"
return $product/name
The where clause filters the results. Only electronics products are returned.
Output:
<name>Wireless Headphones</name>
<name>USB-C Hub</name>
Example 3: Filter by Price and Return Custom XML
for $product in doc("products.xml")/catalog/product
where $product/price > 40
return
<item>
<label>{$product/name/text()}</label>
<cost>{$product/price/text()}</cost>
</item>
This returns products priced above 40 and wraps the results in new custom XML elements. The curly braces { } are used to embed XQuery expressions inside the output structure.
Output:
<item><label>Wireless Headphones</label><cost>79.99</cost></item>
<item><label>Laptop Bag</label><cost>45.00</cost></item>
Example 4: Sort Results with Order By
for $product in doc("products.xml")/catalog/product
order by $product/price ascending
return
<product id="{$product/@id}">
<name>{$product/name/text()}</name>
<price>{$product/price/text()}</price>
</product>
Products are sorted by price from lowest to highest. Note how the attribute value is also constructed using { } expressions.
Example 5: Using the Let Clause
let $items := doc("products.xml")/catalog/product
for $p in $items
where $p/stock > 100
return $p/name
The let clause assigns the product node set to a variable $items for reuse. The query then filters for products with more than 100 units in stock.
Example 6: Aggregate — Count and Sum
let $products := doc("products.xml")/catalog/product
return
<summary>
<totalProducts>{count($products)}</totalProducts>
<totalValue>{sum($products/price)}</totalValue>
</summary>
Output:
<summary>
<totalProducts>4</totalProducts>
<totalValue>172.48</totalValue>
</summary>
XQuery vs XSLT
| Feature | XQuery | XSLT |
|---|---|---|
| Primary purpose | Querying and extracting XML data | Transforming XML into another format |
| Style | Functional, SQL-like syntax | Template-based, declarative XML syntax |
| Best for | XML databases, data retrieval | Presentation, format conversion |
| Output | XML, text, HTML | XML, HTML, text |
| Uses XPath? | Yes | Yes |
XQuery in Practice
XQuery is widely used in:
- XML Databases: Databases like eXist-db and BaseX are queried natively with XQuery.
- Enterprise Systems: Healthcare (HL7/FHIR), finance, and publishing industries use XQuery to process large XML datasets.
- Web Services: Back-end services that exchange XML data can use XQuery to extract and transform responses.
- MarkLogic: A commercial XML database system where XQuery is a primary query language.
Key Points
- XQuery is a language for querying and transforming XML data, analogous to SQL for relational databases.
- The FLWOR expression (
for,let,where,order by,return) is the core construct of XQuery. - Curly braces
{ }embed XQuery expressions inside output XML or text. - XQuery is built on XPath — all XPath expressions work within XQuery.
- It supports aggregation functions like
count()andsum(). - XQuery is the standard query language for XML databases and is widely used in enterprise data processing.
