XML XQuery

Query and extract data from XML documents

🔍 What is XQuery?

XQuery is a powerful query language for XML data, similar to SQL for databases. It allows you to search, extract, and manipulate XML documents efficiently using expressions and functions.


for $book in doc("books.xml")//book
where $book/price < 30
return $book/title
                                    

Key XQuery Concepts

🔎

FLWOR

For, Let, Where, Order, Return

for $x in doc()
return $x
🎯

Path Expressions

Navigate XML structure

doc("file.xml")//book
⚙️

Functions

Built-in data operations

count($books)
🔧

Predicates

Filter results with conditions

//book[price > 20]

🔹 Basic XQuery Syntax

XQuery uses path expressions to navigate XML documents and extract data. The basic syntax is similar to XPath but more powerful. You can select elements, filter data, and create new XML structures from existing documents.

Sample XML:

<library>
    <book id="1">
        <title>XML Guide</title>
        <author>Jane Smith</author>
        <price>25.00</price>
    </book>
    <book id="2">
        <title>Web Development</title>
        <author>Bob Johnson</author>
        <price>35.00</price>
    </book>
</library>

XQuery:

(: Select all book titles :)
doc("library.xml")//book/title

(: Result: :)
<title>XML Guide</title>
<title>Web Development</title>

🔹 FLWOR Expressions

FLWOR (For, Let, Where, Order by, Return) is the core query structure in XQuery. It works like SQL SELECT statements, allowing you to iterate through data, define variables, filter results, sort output, and return formatted data.

for $book in doc("library.xml")//book
let $discount := $book/price * 0.1
where $book/price > 20
order by $book/price descending
return 
    <result>
        <title>{$book/title/text()}</title>
        <original>{$book/price/text()}</original>
        <discounted>{$book/price - $discount}</discounted>
    </result>

Output:

<result>
  <title>Web Development</title>
  <original>35.00</original>
  <discounted>31.50</discounted>
</result>
<result>
  <title>XML Guide</title>
  <original>25.00</original>
  <discounted>22.50</discounted>
</result>

🔹 XQuery Functions

XQuery provides numerous built-in functions for string manipulation, numeric calculations, and data aggregation. These functions help you process and transform XML data efficiently without writing complex logic.

(: Count books :)
count(doc("library.xml")//book)

(: Sum all prices :)
sum(doc("library.xml")//book/price)

(: Average price :)
avg(doc("library.xml")//book/price)

(: String functions :)
upper-case($book/title)
concat($book/author, " - ", $book/title)

(: Conditional :)
if ($book/price > 30) 
then "Expensive" 
else "Affordable"

🔹 Filtering with Predicates

Predicates filter XML elements based on conditions. They are enclosed in square brackets and can test element values, attributes, or positions. Multiple predicates can be combined for complex filtering.

(: Books with price less than 30 :)
doc("library.xml")//book[price < 30]

(: Books by specific author :)
doc("library.xml")//book[author = "Jane Smith"]

(: First book :)
doc("library.xml")//book[1]

(: Books with id attribute :)
doc("library.xml")//book[@id]

(: Multiple conditions :)
doc("library.xml")//book[price > 20 and price < 40]

🔹 Creating New XML

XQuery can construct new XML documents from existing data. You can create custom structures, combine data from multiple sources, and format output according to your needs using element constructors.

<bookstore>
{
    for $book in doc("library.xml")//book
    where $book/price < 30
    return
        <item>
            <name>{$book/title/text()}</name>
            <cost>{$book/price/text()}</cost>
            <writer>{$book/author/text()}</writer>
        </item>
}
</bookstore>

🧠 Test Your Knowledge

What does FLWOR stand for in XQuery?