Welcome to The Coding College! In this guide, we’ll explore XQuery through practical examples to help you understand how to query and manipulate XML data effectively. If you’ve ever worked with XML and wanted a powerful way to extract and process data, XQuery is the tool you need.
What is XQuery?
XQuery is a query language specifically designed for XML documents. It allows you to:
- Retrieve specific elements or attributes from XML files.
- Filter, sort, and aggregate data.
- Restructure XML into new formats.
- Work with hierarchical data effectively.
This tutorial will cover real-world examples to showcase its capabilities.
Example 1: Retrieving Data from an XML Document
Input XML: books.xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>Learning XML</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book>
<title>Advanced XSLT</title>
<author>Jane Smith</author>
<price>39.99</price>
</book>
</library>
Query: Extracting Book Titles
for $book in doc("books.xml")/library/book
return $book/title
Output:
<title>Learning XML</title>
<title>Advanced XSLT</title>
Example 2: Filtering Data
Let’s filter books priced below 35.
Query:
for $book in doc("books.xml")/library/book
where $book/price < 35
return $book/title
Output:
<title>Learning XML</title>
Example 3: Sorting Data
Let’s sort books by their price in ascending order.
Query:
for $book in doc("books.xml")/library/book
order by $book/price
return
<book>
<title>{ $book/title }</title>
<price>{ $book/price }</price>
</book>
Output:
<book>
<title>Learning XML</title>
<price>29.99</price>
</book>
<book>
<title>Advanced XSLT</title>
<price>39.99</price>
</book>
Example 4: Creating a New XML Structure
XQuery can be used to create entirely new XML documents based on existing data.
Query:
<discountedBooks>
{
for $book in doc("books.xml")/library/book
where $book/price > 30
return
<book>
<title>{ $book/title }</title>
<discountedPrice>{ $book/price * 0.9 }</discountedPrice>
</book>
}
</discountedBooks>
Output:
<discountedBooks>
<book>
<title>Advanced XSLT</title>
<discountedPrice>35.991</discountedPrice>
</book>
</discountedBooks>
Example 5: Aggregating Data
Let’s calculate the total price of all books.
Query:
<totalPrice>
{
sum(doc("books.xml")/library/book/price)
}
</totalPrice>
Output:
<totalPrice>69.98</totalPrice>
Example 6: Grouping Data
If the XML document contains categories, you can group books by category.
Input XML:
<library>
<book>
<title>Learning XML</title>
<category>Programming</category>
<price>29.99</price>
</book>
<book>
<title>Advanced XSLT</title>
<category>Programming</category>
<price>39.99</price>
</book>
<book>
<title>Cooking for Beginners</title>
<category>Cooking</category>
<price>19.99</price>
</book>
</library>
Query: Grouping Books by Category
for $category in distinct-values(doc("books.xml")/library/book/category)
return
<category name="{ $category }">
{
for $book in doc("books.xml")/library/book
where $book/category = $category
return $book/title
}
</category>
Output:
<category name="Programming">
<title>Learning XML</title>
<title>Advanced XSLT</title>
</category>
<category name="Cooking">
<title>Cooking for Beginners</title>
</category>
Example 7: Joining Data
If you have multiple XML files, you can join their data using XQuery.
Input XML: books.xml
<library>
<book>
<id>1</id>
<title>Learning XML</title>
</book>
<book>
<id>2</id>
<title>Advanced XSLT</title>
</book>
</library>
Input XML: prices.xml
<prices>
<price>
<bookId>1</bookId>
<amount>29.99</amount>
</price>
<price>
<bookId>2</bookId>
<amount>39.99</amount>
</price>
</prices>
Query: Joining Books and Prices
for $book in doc("books.xml")/library/book,
$price in doc("prices.xml")/prices/price
where $book/id = $price/bookId
return
<book>
<title>{ $book/title }</title>
<price>{ $price/amount }</price>
</book>
Output:
<book>
<title>Learning XML</title>
<price>29.99</price>
</book>
<book>
<title>Advanced XSLT</title>
<price>39.99</price>
</book>
Tools for Running XQuery
- BaseX: A lightweight XML database that supports XQuery.
- Saxon: One of the most popular XQuery processors.
- eXist-db: Open-source XML database with XQuery support.
- Altova XMLSpy: A powerful XML editor with integrated XQuery support.
Conclusion
With XQuery, you can perform complex operations on XML data efficiently, whether it’s filtering, sorting, aggregating, or transforming the data. These examples demonstrate the versatility and power of XQuery in handling XML-based workflows.
For more tutorials and resources, visit The Coding College, your hub for mastering programming concepts and technologies.