XQuery Aggregate Functions: Mastering Data Analysis in XML

beginner
25 min

XQuery Aggregate Functions: Mastering Data Analysis in XML

Welcome back to CodeYourCraft! Today, we're diving into XQuery Aggregate Functions. These powerful tools will help you analyze and manipulate your XML data in a breeze. Let's get started! šŸš€

What are XQuery Aggregate Functions? šŸ’”

XQuery Aggregate Functions are a set of functions used to perform calculations and generate summaries from XML data. They're similar to SQL's aggregate functions but designed specifically for XML.

šŸ“ Note: XQuery Aggregate Functions can help you answer questions like:

  • What's the total number of books in a library?
  • What's the average price of all books?
  • Which author has written the most books?

Understanding the Basics šŸŽÆ

Before we dive into specific functions, let's discuss the for, let, and order by clauses, which are essential when working with aggregate functions.

for Clause

The for clause is used to loop through a sequence of nodes.

xquery
for $book in doc("books.xml")/books/book

let Clause

The let clause is used to create variables in XQuery.

xquery
let $totalPrice := 0 for $book in doc("books.xml")/books/book let $price := $book/price return $totalPrice + $price

order by Clause

The order by clause is used to sort the sequence of nodes.

xquery
for $book in doc("books.xml")/books/book order by $book/title

XQuery Aggregate Functions šŸ’”

Now that we've covered the basics, let's explore some common XQuery Aggregate Functions.

count() šŸŽÆ

The count() function returns the number of items in a sequence.

xquery
count(doc("books.xml")/books/book)

sum() šŸŽÆ

The sum() function returns the sum of the values in a sequence.

xquery
let $totalPrice := 0 for $book in doc("books.xml")/books/book let $price := $book/price return $totalPrice + $price

avg() šŸŽÆ

The avg() function returns the average of the values in a sequence.

xquery
let $totalPrice := 0 for $book in doc("books.xml")/books/book let $price := $book/price return $totalPrice + $price return avg($totalPrice)

min() and max() šŸŽÆ

The min() and max() functions return the minimum and maximum values in a sequence, respectively.

xquery
let $minPrice := 99999 for $book in doc("books.xml")/books/book let $price := $book/price if ($price < $minPrice) then $minPrice := $price return $minPrice

Practical Example šŸ“

Let's analyze the number of books written by each author in a sample XML file.

books.xml

xml
<books> <book> <title>The Catcher in the Rye</title> <author>J.D. Salinger</author> <price>12.99</price> </book> <book> <title>To Kill a Mockingbird</title> <author>Harper Lee</author> <price>10.99</price> </book> <book> <title>Of Mice and Men</title> <author>John Steinbeck</author> <price>9.99</price> </book> </books>
xquery
for $author in distinct-values(doc("books.xml")/books/book/author) let $bookCount := 0 for $book in doc("books.xml")/books/book[author=$author] return <author>{$author}</author> <books>{$bookCount}</books> let $bookCount := $bookCount + 1 return ()

This example will produce the following output:

xml
<author>Harper Lee</author> <books>1</books> <author>John Steinbeck</author> <books>1</books> <author>J.D. Salinger</author> <books>1</books>

šŸ“ Note: The distinct-values() function is used to remove duplicate values.

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

What does the `count()` function do in XQuery?

Quick Quiz
Question 1 of 1

What does the `sum()` function do in XQuery?

That's it for today's lesson on XQuery Aggregate Functions! Stay tuned for more tutorials at CodeYourCraft. Happy coding! šŸ¤“šŸš€