Chapter 15: XML XQuery

1. What is XQuery? (The clearest honest explanation)

XQuery = XML Query Language (pronounced “X-query”)

It is a standard query language designed specifically to:

  • Search
  • Extract
  • Transform
  • Combine
  • Calculate over … one or more XML documents

Think of XQuery as SQL for XML — but more powerful and more flexible.

While XPath is mainly used to select nodes (like a path expression),

XQuery is a full programming language that can:

  • do loops
  • make conditions
  • define variables & functions
  • perform calculations
  • create new XML structures
  • join multiple documents
  • sort results
  • group data … and much more

Analogy most people understand quickly:

  • XPath = the address on an envelope (“go to library → book → title”)
  • XQuery = the full letter you write (“go to the library, find all books published after 2000, sort them by title, and make me a nice table with title + author + price”)

2. XPath vs XQuery – Quick Comparison Table (very important!)

Feature XPath XQuery
Main purpose Select nodes / paths Query + transform + construct new XML
Can create new elements? No Yes
Loops (for / let) No (only limited predicates) Yes
Variables Very limited Yes (let $var := …)
Functions & user-defined funcs No Yes
Sorting Very limited Yes (order by)
Grouping No Yes (group by in XQuery 3.0+)
Joins between documents Hard / limited Easy
FLWOR expression No Yes (for-let-where-order-return)
Typical use Inside XSLT, DOM, configuration Reporting, data integration, XML databases

Rule of thumb people use in 2025–2026:

  • Need only to find / select nodes → XPath
  • Need to build new structure, calculate, join, sort, groupXQuery

3. Very First Simple Example – Selecting & Formatting

Input XML (books.xml)

XML

Very simple XQuery (select all titles)

xquery
for $book in /library/book
return $book/title

Output (sequence of text nodes):

text

Slightly nicer version (with formatting)

xquery
for $book in /library/book
return
<li>{$book/title/string()} by {$book/author/string()}</li>

Result:

XML

4. The Heart of XQuery: FLWOR Expression

FLWOR = For – Let – Where – Order by – Return (the most common pattern in real XQuery code)

Example – books after 2000, sorted by title, with calculated discount

xquery
for $book in /library/book
let $price := number($book/@price)
let $discounted := $price * 0.85
where $book/@year >= 2000
order by $book/title ascending
return
<book-result>
<title>{$book/title/string()}</title>
<author>{$book/author/string()}</author>
<original-price>{$price}</original-price>
<discounted-price>{round($discounted, 2)}</discounted-price>
</book-result>

Output:

XML

5. More Realistic Example – Report from Order Data

Input XML (orders.xml)

XML

XQuery – Generate summary report

xquery
declare variable $orders := doc("orders.xml")/orders;

<summary-report generated="{current-date()}">
{
for $order in $orders/order
let $total := sum($order/item/(@qty * @price))
let $item-count := count($order/item)
where $total > 50000
order by $total descending
return
<order-summary>
<order-id>{$order/@id/string()}</order-id>
<date>{$order/@date/string()}</date>
<item-count>{$item-count}</item-count>
<grand-total currency="INR">{round($total, 2)}</grand-total>
<high-value>{"YES" if ($total > 100000) else "NO"}</high-value>
</order-summary>
}
</summary-report>

Output:

XML

6. Very Common Real-World Patterns

Use case Typical XQuery pattern
Generate HTML report Return <html><table>{for $row …}</table></html>
Create JSON (XQuery 3.0+) json:serialize( map { … } )
Join two documents for $o in doc(“orders”)//order, $c in doc(“customers”)//customer where $o/@customer = $c/@id return …
Group by category (XQuery 3.0) for $cat in distinct-values(//category) group by $cat return …
Filter + calculate let $total := sum(//item/(@qty * @price))
Transform XML to different namespace Use constructor + namespace declarations

Quick Summary – XQuery Cheat Sheet

Goal Typical expression
Loop over elements for $x in //book
Define variable let $total := sum(//price)
Filter where $x/@year > 2000
Sort order by $x/title ascending
Build new XML <result><title>{$x/title}</title></result>
Get text value $x/title/string() or data($x/title)
Count items count(//item)
Sum / avg / min / max sum(//price), avg(//price)

Would you like to continue with one of these topics?

  • XQuery 3.0 / 3.1 features (group by, arrays, maps, JSON support)
  • Joining multiple XML files (very common in real projects)
  • How to run XQuery (tools, BaseX, Saxon, Zorba, eXist-db, command line)
  • XQuery inside XSLT (yes – you can mix them)
  • XQuery vs SQL/XML vs XSLT – when to choose what
  • Real example from e-invoice / EDI / healthcare / catalog data

Just tell me which direction feels most useful or interesting for you right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *