Welcome to The Coding College! In this guide, we’ll explore XQuery functions, which are powerful tools for processing, transforming, and querying XML data efficiently. Functions in XQuery allow you to perform complex operations, reuse code, and make your queries more modular and maintainable.
What Are XQuery Functions?
XQuery functions are reusable blocks of code that perform specific tasks on XML data. Functions can be either:
- Built-in functions: Predefined functions provided by XQuery.
- User-defined functions: Custom functions created by you for specific purposes.
Functions help you:
- Simplify repetitive tasks.
- Perform advanced XML transformations.
- Make your queries more readable and modular.
Built-In Functions in XQuery
XQuery includes a wide range of built-in functions that handle strings, numbers, dates, sequences, and more.
Categories of Built-In Functions
- String Functions: For manipulating text.
- Sequence Functions: For working with sequences of nodes.
- Numeric Functions: For mathematical calculations.
- Date and Time Functions: For handling date and time values.
- Boolean Functions: For logical operations.
Examples of Built-In Functions
1. String Functions
fn:contains()
: Checks if a string contains a substring.
Example:
for $book in doc("library.xml")/library/book
where contains($book/title, "XML")
return $book/title
Output:
<title>Learning XML</title>
fn:concat()
: Combines multiple strings.
Example:
for $book in doc("library.xml")/library/book
return concat($book/title, " - ", $book/author)
Output:
Learning XML - John Doe
Mastering XQuery - Jane Smith
2. Numeric Functions
fn:sum()
: Calculates the sum of numeric values.
Example:
let $prices := doc("library.xml")/library/book/price
return sum($prices)
Output:
69.98
fn:round()
: Rounds a number to the nearest whole number.
Example:
round(29.99)
Output:
30
3. Sequence Functions
fn:count()
: Counts the number of items in a sequence.
Example:
count(doc("library.xml")/library/book)
Output:
2
fn:distinct-values()
: Returns distinct values from a sequence.
Example:
distinct-values(doc("library.xml")/library/book/category)
Output:
Programming
Databases
4. Date and Time Functions
fn:current-date()
: Returns the current date.
Example:
current-date()
Output:
2024-12-27
5. Boolean Functions
fn:empty()
: Checks if a sequence is empty.
Example:
if (empty(doc("library.xml")/library/magazine)) then "No magazines" else "Magazines available"
Output:
No magazines
Creating User-Defined Functions
XQuery allows you to create custom functions to perform specific tasks.
Syntax for User-Defined Functions
declare function local:function-name($parameter as type) as return-type {
(: function body :)
};
Example 1: Simple Function
Create a function to calculate the discounted price of a book.
XQuery Code:
declare function local:discounted-price($price as xs:decimal, $discount as xs:decimal) as xs:decimal {
$price * (1 - $discount)
};
for $book in doc("library.xml")/library/book
return
<book>
{ $book/title }
<discounted-price>{ local:discounted-price($book/price, 0.1) }</discounted-price>
</book>
Output:
<book>
<title>Learning XML</title>
<discounted-price>26.991</discounted-price>
</book>
<book>
<title>Mastering XQuery</title>
<discounted-price>35.991</discounted-price>
</book>
Example 2: Function with Conditional Logic
Create a function to categorize books by price range.
XQuery Code:
declare function local:price-category($price as xs:decimal) as xs:string {
if ($price > 30) then "Expensive"
else "Affordable"
};
for $book in doc("library.xml")/library/book
return
<book>
{ $book/title }
<category>{ local:price-category($book/price) }</category>
</book>
Output:
<book>
<title>Learning XML</title>
<category>Affordable</category>
</book>
<book>
<title>Mastering XQuery</title>
<category>Expensive</category>
</book>
Example 3: Reusing Functions
Functions can call other functions for modular and reusable code.
XQuery Code:
declare function local:discounted-price($price as xs:decimal, $discount as xs:decimal) as xs:decimal {
$price * (1 - $discount)
};
declare function local:book-info($title as xs:string, $price as xs:decimal) as element(book) {
<book>
<title>{ $title }</title>
<original-price>{ $price }</original-price>
<discounted-price>{ local:discounted-price($price, 0.1) }</discounted-price>
</book>
};
for $book in doc("library.xml")/library/book
return local:book-info($book/title, $book/price)
Output:
<book>
<title>Learning XML</title>
<original-price>29.99</original-price>
<discounted-price>26.991</discounted-price>
</book>
<book>
<title>Mastering XQuery</title>
<original-price>39.99</original-price>
<discounted-price>35.991</discounted-price>
</book>
Best Practices for Using XQuery Functions
- Use Descriptive Names: Ensure function names clearly describe their purpose.
- Keep Functions Modular: Break down complex logic into smaller functions.
- Leverage Parameters: Pass dynamic values to functions for flexibility.
- Reuse Functions: Avoid repeating logic by reusing functions wherever possible.
Tools for Practicing XQuery Functions
- BaseX: A lightweight and fast XML database.
- eXist-db: Open-source XML database with excellent XQuery support.
- Saxon: A comprehensive XQuery processor.
Conclusion
Mastering XQuery functions is key to efficiently handling XML data. Built-in functions provide powerful tools for common operations, while user-defined functions allow you to customize and extend XQuery for specific needs.
For more tutorials, examples, and resources, visit The Coding College. Keep coding, keep learning!