XQuery Functions
Built-in and custom functions for data manipulation
⚡ What are XQuery Functions?
XQuery functions are reusable code blocks that perform specific operations on data. They include built-in functions for strings, numbers, dates, and sequences, plus the ability to create custom functions for specialized tasks.
(: Using a built-in function :)
count(//book)
concat("Hello", " ", "World")
Function Categories
XQuery provides a rich library of built-in functions organized by data type and purpose. These functions simplify common operations and data transformations.
String Functions
Manipulate text data
concat(), substring()
Numeric Functions
Perform calculations
sum(), avg(), round()
Sequence Functions
Work with collections
count(), distinct-values()
Custom Functions
Create your own functions
declare function
🔹 String Functions
Common functions for text manipulation:
(: Concatenate strings :)
concat("Hello", " ", "World")
(: Result: "Hello World" :)
(: Get string length :)
string-length("XQuery")
(: Result: 6 :)
(: Extract substring :)
substring("XQuery", 1, 3)
(: Result: "XQu" :)
(: Convert to uppercase :)
upper-case("hello")
(: Result: "HELLO" :)
(: Convert to lowercase :)
lower-case("HELLO")
(: Result: "hello" :)
(: Check if contains :)
contains("XQuery", "Query")
(: Result: true :)
🔹 Numeric Functions
Functions for mathematical operations:
(: Sum of values :)
sum(//book/price)
(: Result: 149.95 :)
(: Average value :)
avg(//book/price)
(: Result: 29.99 :)
(: Maximum value :)
max(//book/price)
(: Result: 39.99 :)
(: Minimum value :)
min(//book/price)
(: Result: 19.99 :)
(: Round number :)
round(29.567)
(: Result: 30 :)
(: Ceiling and floor :)
ceiling(29.1) (: 30 :)
floor(29.9) (: 29 :)
Example:
let $prices := (19.99, 29.99, 39.99)
return
<summary>
<total>{sum($prices)}</total>
<average>{avg($prices)}</average>
</summary>
🔹 Sequence Functions
Functions for working with sequences:
(: Count items :)
count(//book)
(: Result: 5 :)
(: Get distinct values :)
distinct-values(//book/author)
(: Result: unique authors :)
(: Check if empty :)
empty(//book[@id="999"])
(: Result: true :)
(: Check if exists :)
exists(//book[@id="1"])
(: Result: true :)
(: Reverse sequence :)
reverse((1, 2, 3, 4, 5))
(: Result: (5, 4, 3, 2, 1) :)
(: Remove duplicates :)
distinct-values((1, 2, 2, 3, 3, 3))
(: Result: (1, 2, 3) :)
🔹 Boolean Functions
Functions that return true or false:
(: Check if true :)
boolean(//book[@price < 30])
(: Result: true if any book under $30 :)
(: Not function :)
not(empty(//book))
(: Result: true if books exist :)
(: Starts with :)
starts-with("XQuery", "XQ")
(: Result: true :)
(: Ends with :)
ends-with("XQuery", "ery")
(: Result: true :)
🔹 Node Functions
Functions for working with XML nodes:
(: Get node name :)
name(//book[1])
(: Result: "book" :)
(: Get local name :)
local-name(//book[1])
(: Result: "book" :)
(: Get text content :)
data(//book/title)
(: Result: title text values :)
(: Get string value :)
string(//book[1]/title)
(: Result: first book title as string :)
🔹 Custom Functions
Create your own reusable functions:
(: Declare a custom function :)
declare function local:discount($price) {
$price * 0.9
};
(: Use the function :)
for $book in //book
return
<book>
<title>{$book/title}</title>
<original>{$book/price}</original>
<discounted>{local:discount($book/price)}</discounted>
</book>
(: Function with multiple parameters :)
declare function local:fullName($first, $last) {
concat($first, " ", $last)
};
local:fullName("John", "Doe")
(: Result: "John Doe" :)
🔹 Aggregate Functions
Functions for data aggregation:
(: Total price of all books :)
<total>{sum(//book/price)}</total>
(: Average price by category :)
for $cat in distinct-values(//book/@category)
return
<category name="{$cat}">
<avgPrice>{avg(//book[@category=$cat]/price)}</avgPrice>
<count>{count(//book[@category=$cat])}</count>
</category>