XML and XQuery

Welcome to The Coding College, your go-to destination for programming tutorials! In this article, we’ll explore XML and XQuery, an efficient and robust toolset for querying XML data. Whether you’re extracting specific information or transforming XML into a new structure, XQuery is an invaluable tool for developers working with XML.

What is XQuery?

XQuery (XML Query Language) is a powerful language designed for querying, filtering, and transforming XML data. It is particularly useful for handling large and complex XML documents and databases.

Why Use XQuery?

  1. Efficient Data Retrieval: Query specific parts of XML documents or databases quickly.
  2. Cross-Platform: Supported in many XML databases and programming environments.
  3. Versatility: Perform operations like filtering, sorting, grouping, and joining XML data.
  4. Ease of Use: Provides SQL-like syntax for XML, making it easy to learn for developers familiar with relational databases.

XQuery vs XPath

XQuery builds on XPath, the XML Path Language. While XPath is primarily used for locating nodes in an XML document, XQuery extends its capabilities to include:

  • Filtering: Query only specific parts of the document based on conditions.
  • Sorting and Grouping: Arrange or aggregate data for better insights.
  • Data Transformation: Restructure XML documents into different formats or structures.

Example Comparison

FunctionalityXPathXQuery
Extract all <title> tags//titlefor $t in //title return $t
Perform CalculationsNot directly possiblefor $b in //book return $b/price * 1.2
Transform XMLLimitedFully supported

Example XML Document

<library>
    <book id="1">
        <title>XML Basics</title>
        <author>John Doe</author>
        <price>19.99</price>
    </book>
    <book id="2">
        <title>Advanced XML</title>
        <author>Jane Smith</author>
        <price>29.99</price>
    </book>
</library>

Writing an XQuery

XQuery uses FLWOR (For, Let, Where, Order by, Return) expressions to query XML. Let’s break it down with examples.

Basic XQuery Example

Extract all book titles from the XML document:

for $book in //book
return $book/title

Output:

<title>XML Basics</title>
<title>Advanced XML</title>

Using Conditions (Filtering)

Retrieve books priced over $20:

for $book in //book
where $book/price > 20
return $book/title

Output:

<title>Advanced XML</title>

Performing Calculations

Add 10% tax to all book prices:

for $book in //book
return <book>
    <title>{ $book/title }</title>
    <price-with-tax>{ $book/price * 1.1 }</price-with-tax>
</book>

Output:

<book>
    <title>XML Basics</title>
    <price-with-tax>21.989</price-with-tax>
</book>
<book>
    <title>Advanced XML</title>
    <price-with-tax>32.989</price-with-tax>
</book>

Sorting Results

Sort books by price in ascending order:

for $book in //book
order by $book/price
return $book/title

Output:

<title>XML Basics</title>
<title>Advanced XML</title>

Grouping Results

Count the number of books by each author:

for $author in distinct-values(//book/author)
let $count := count(//book[author = $author])
return <author>
    <name>{ $author }</name>
    <book-count>{ $count }</book-count>
</author>

Output:

<author>
    <name>John Doe</name>
    <book-count>1</book-count>
</author>
<author>
    <name>Jane Smith</name>
    <book-count>1</book-count>
</author>

Using XQuery in Practice

1. XQuery in XML Databases

XML databases like eXist-db, BaseX, and MarkLogic use XQuery for querying and managing large XML datasets.

Example Query in eXist-db:

xquery version "3.1";
for $book in doc("/db/library.xml")//book
where $book/price > 20
return $book/title

2. XQuery in Python

Python’s lxml library allows basic XQuery functionality.

from lxml import etree

# Sample XML
xml_data = '''
<library>
    <book id="1">
        <title>XML Basics</title>
        <author>John Doe</author>
        <price>19.99</price>
    </book>
    <book id="2">
        <title>Advanced XML</title>
        <author>Jane Smith</author>
        <price>29.99</price>
    </book>
</library>
'''

# Parse XML
root = etree.XML(xml_data)

# XPath (basic XQuery-like functionality)
titles = root.xpath("//book[price > 20]/title/text()")
print(titles)  # Output: ['Advanced XML']

3. XQuery in Java

Java’s Saxon library provides full XQuery support.

import net.sf.saxon.s9api.*;

public class XQueryExample {
    public static void main(String[] args) throws Exception {
        String xml = """
        <library>
            <book id="1">
                <title>XML Basics</title>
                <author>John Doe</author>
                <price>19.99</price>
            </book>
            <book id="2">
                <title>Advanced XML</title>
                <author>Jane Smith</author>
                <price>29.99</price>
            </book>
        </library>
        """;

        String query = """
        for $book in //book
        where $book/price > 20
        return $book/title
        """;

        Processor processor = new Processor(false);
        XQueryCompiler compiler = processor.newXQueryCompiler();
        XQueryExecutable executable = compiler.compile(query);
        XQueryEvaluator evaluator = executable.load();

        evaluator.setContextItem(processor.newDocumentBuilder().build(new StreamSource(new StringReader(xml))));
        evaluator.run(new StreamResult(System.out));
    }
}

Benefits of Using XQuery

  1. Flexibility: Query and manipulate XML documents with SQL-like ease.
  2. High Performance: Ideal for querying large XML datasets or databases.
  3. Advanced Operations: Perform joins, aggregations, and calculations.
  4. Cross-Compatibility: Works with XML databases, APIs, and programming languages.

Applications of XML and XQuery

  1. Web Services: Querying and transforming XML-based APIs (e.g., SOAP).
  2. Content Management: Extracting data from XML-based CMS systems.
  3. Data Analysis: Aggregating and filtering data stored in XML format.
  4. Database Queries: Querying XML databases with powerful filters and transformations.

Learn More at The Coding College

For more programming tutorials, including in-depth guides on XML, XQuery, and related technologies, visit The Coding College. Our articles are tailored to help developers of all skill levels achieve success.

Conclusion

XQuery is a powerful tool for querying, transforming, and working with XML data. Whether you’re managing XML in databases, APIs, or standalone documents, mastering XQuery is essential for modern XML-based workflows.

Leave a Comment