XQuery: Selecting and Filtering

Welcome to The Coding College! In this tutorial, we’ll dive into how to use XQuery for selecting and filtering data from XML documents. These are fundamental skills for efficiently querying and transforming XML content.

Why Use Selecting and Filtering in XQuery?

When working with XML data, it’s often necessary to:

  • Select specific elements, attributes, or nodes.
  • Filter out irrelevant data based on conditions.
  • Extract only the information you need for processing or displaying.

XQuery provides robust tools for performing these tasks.

Selecting Elements and Attributes

XQuery uses XPath expressions to navigate and select elements or attributes from an XML document.

Example 1: Selecting Elements

Input XML (library.xml):

<library>
  <book>
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Mastering XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

XQuery Code:

for $book in doc("library.xml")/library/book
return $book/title

Output:

<title>Learning XML</title>
<title>Mastering XQuery</title>

Example 2: Selecting Attributes

XQuery Code:

for $book in doc("library.xml")/library/book
return <price>{$book/price}</price>

Output:

<price>29.99</price>
<price>39.99</price>

Filtering Data

Filtering allows you to select only those nodes that meet specific conditions using where clauses.

Example 3: Filtering with Conditions

Select books where the price is greater than 30.

XQuery Code:

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

Output:

<title>Mastering XQuery</title>

Example 4: Filtering by Attribute Value

Suppose you add categories as attributes to the <book> elements:

Updated Input XML:

<library>
  <book category="Programming">
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book category="Databases">
    <title>Mastering XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

XQuery Code:

for $book in doc("library.xml")/library/book[@category="Programming"]
return $book/title

Output:

<title>Learning XML</title>

Combining Selection and Filtering

XQuery allows you to combine selection and filtering for precise results.

Example 5: Select Specific Elements with Conditions

Select the titles and prices of books priced above 30.

XQuery Code:

for $book in doc("library.xml")/library/book
where $book/price > 30
return
  <book-info>
    { $book/title }
    { $book/price }
  </book-info>

Output:

<book-info>
  <title>Mastering XQuery</title>
  <price>39.99</price>
</book-info>

Example 6: Filtering with Multiple Conditions

You can use logical operators like and, or, and not for more complex filtering.

XQuery Code:

for $book in doc("library.xml")/library/book
where $book/price > 20 and $book/@category = "Programming"
return $book/title

Output:

<title>Learning XML</title>

Sorting Results

Sorting is another important feature when selecting and filtering data.

Example 7: Ordering by Price

Sort the books by price in ascending order.

XQuery Code:

for $book in doc("library.xml")/library/book
order by $book/price
return
  <book>
    { $book/title }
    { $book/price }
  </book>

Output:

<book>
  <title>Learning XML</title>
  <price>29.99</price>
</book>
<book>
  <title>Mastering XQuery</title>
  <price>39.99</price>
</book>

Using XPath Axes for Selection

XPath axes allow you to select nodes relative to other nodes.

Example 8: Parent and Child Nodes

Select the parent <book> node of a specific title.

XQuery Code:

for $book in doc("library.xml")/library/book
where $book/title = "Learning XML"
return $book

Output:

<book category="Programming">
  <title>Learning XML</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

Dynamic Filtering

Sometimes, filtering criteria can be dynamic and based on user input or other variables.

Example 9: Using Variables for Filtering

XQuery Code:

declare variable $maxPrice := 30;

for $book in doc("library.xml")/library/book
where $book/price <= $maxPrice
return $book/title

Output:

<title>Learning XML</title>

Tools for Practicing XQuery

Here are some tools you can use to test and practice selecting and filtering with XQuery:

  1. BaseX: A lightweight XML database with a powerful query editor.
  2. eXist-db: An open-source XML database with robust XQuery support.
  3. Saxon: A popular processor for XSLT and XQuery.
  4. Altova XMLSpy: A comprehensive XML editor with XQuery capabilities.

Conclusion

XQuery’s ability to select and filter data makes it an essential tool for working with XML documents. Whether you’re extracting specific information, applying complex conditions, or preparing XML data for transformation, XQuery provides the tools you need.

For more resources and tutorials, visit The Coding College and continue your journey toward mastering XML and XQuery!

Leave a Comment