XQuery Example

Practical XQuery examples for beginners

💡 XQuery in Practice

Learn XQuery through hands-on examples. These practical scenarios demonstrate how to query XML data, filter results, perform calculations, and create new XML structures from existing documents.


(: Basic query :)
for $x in doc("data.xml")//item
return $x
                                    

Common XQuery Tasks

XQuery handles various data operations efficiently. From simple data extraction to complex filtering and calculations, these examples show real-world applications you'll encounter when working with XML documents.

📝

Select Data

Extract specific elements

🔍

Filter Results

Find matching items

🔢

Calculate

Perform math operations

🏗️

Build XML

Create new structures

🔹 Example 1: Select All Books

Retrieve all book titles from an XML document:

📄 books.xml:

<bookstore>
    <book category="web">
        <title>Learning HTML</title>
        <author>John Doe</author>
        <price>29.99</price>
    </book>
    <book category="programming">
        <title>JavaScript Basics</title>
        <author>Jane Smith</author>
        <price>39.99</price>
    </book>
</bookstore>

📄 XQuery:

(: Select all book titles :)
for $book in doc("books.xml")//book
return $book/title

Result:

<title>Learning HTML</title>
<title>JavaScript Basics</title>

🔹 Example 2: Filter by Price

Find books cheaper than $35:

📄 XQuery:

(: Books under $35 :)
for $book in doc("books.xml")//book
where $book/price < 35
return 
    <affordable>
        {$book/title}
        {$book/price}
    </affordable>

Result:

<affordable>
    <title>Learning HTML</title>
    <price>29.99</price>
</affordable>

🔹 Example 3: Sort Books

Sort books by price in ascending order:

📄 XQuery:

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

Result:

<book>
    <title>Learning HTML</title>
    <price>29.99</price>
</book>
<book>
    <title>JavaScript Basics</title>
    <price>39.99</price>
</book>

🔹 Example 4: Calculate Total

Calculate the total price of all books:

📄 XQuery:

(: Sum all prices :)
<total>
    {sum(doc("books.xml")//book/price)}
</total>

Result:

<total>69.98</total>

🔹 Example 5: Filter by Category

Get books from a specific category:

📄 XQuery:

(: Web development books only :)
for $book in doc("books.xml")//book[@category="web"]
return 
    <webbook>
        <title>{$book/title/text()}</title>
        <author>{$book/author/text()}</author>
    </webbook>

Result:

<webbook>
    <title>Learning HTML</title>
    <author>John Doe</author>
</webbook>

🔹 Example 6: Count Books

Count total number of books:

📄 XQuery:

(: Count all books :)
<statistics>
    <total-books>{count(doc("books.xml")//book)}</total-books>
    <categories>{count(distinct-values(doc("books.xml")//book/@category))}</categories>
</statistics>

Result:

<statistics>
    <total-books>2</total-books>
    <categories>2</categories>
</statistics>

🔹 Example 7: String Operations

Manipulate text data in queries:

📄 XQuery:

(: Convert titles to uppercase :)
for $book in doc("books.xml")//book
return 
    <book>
        <title>{upper-case($book/title)}</title>
        <author>{$book/author/text()}</author>
    </book>

Result:

<book>
    <title>LEARNING HTML</title>
    <author>John Doe</author>
</book>
<book>
    <title>JAVASCRIPT BASICS</title>
    <author>Jane Smith</author>
</book>

🔹 Example 8: Conditional Output

Use if-then-else logic in queries:

(: Label books as cheap or expensive :)
for $book in doc("books.xml")//book
return 
    <book>
        {$book/title}
        <label>
        {
            if ($book/price < 35) 
            then "Affordable"
            else "Premium"
        }
        </label>
    </book>

🔹 Useful XQuery Tips

  • Comments: Use (: comment :) for notes
  • Variables: Start with $ like $book
  • Paths: Use // to search anywhere in document
  • Predicates: Use [] for filtering conditions
  • Text: Use text() to get text content only
  • Attributes: Use @ to access attributes

🧠 Test Your Knowledge

Which XQuery function counts elements?