XQuery Functions: A Comprehensive Guide to Data Manipulation

Master XQuery's built-in functions for efficient XML data manipulation. This guide provides a categorized overview of commonly used functions, covering string manipulation, mathematical operations, data type conversions, node processing, and more, enhancing your XQuery skills for effective XML data management.



XQuery Functions: A Comprehensive Guide

XQuery provides a rich set of built-in functions for various data manipulation tasks. These functions operate on different data types (numbers, strings, booleans, dates, nodes, sequences, etc.). This guide categorizes and explains some of the most commonly used XQuery functions.

Function Namespace and Syntax

XQuery functions typically use the namespace prefix `fn:` (e.g., `fn:string()`). However, `fn:` is often the default namespace, so you may be able to omit it. The general syntax is:


fn:function-name(argument1, argument2, ...);

Accessor Functions

Accessor functions retrieve information about nodes or data values.

Index Function Description
1 fn:node-name(node) Returns the name of a node.
2 fn:nilled(node) Checks if a node is nil (returns a boolean).
3 fn:data(item1, item2, ...) Converts a sequence of items to atomic values.
4 fn:base-uri(), fn:base-uri(node) Returns the base URI of the current or specified node.
5 fn:document-uri(node) Returns the URI of the document containing the specified node.

Error and Trace Functions

These functions are helpful for debugging and error handling.

Index Function Description
1 fn:error() Raises an error.
2 fn:trace(value, label) Outputs a debugging message (used for tracing the query execution).

Numeric Functions

These functions perform mathematical operations.

Index Function Description
1 fn:abs(number) Returns the absolute value.
2 fn:ceiling(number) Returns the smallest integer greater than or equal to the number.
3 fn:floor(number) Returns the largest integer less than or equal to the number.
4 fn:round(number) Rounds to the nearest integer.
5 fn:round-half-to-even(number) Rounds to the nearest integer using banker's rounding.

String Functions

XQuery provides a wide range of functions for string manipulation. Here are a few key ones:

Index Function Description
1 fn:string-length(string) Returns the length of a string.
2 fn:concat(string1, string2, ...) Concatenates strings.
3 fn:string-join(sequence, delimiter) Joins strings in a sequence with a delimiter.

Boolean Functions

Index Function Description
1 fn:boolean(value) Converts a value to a boolean.
2 fn:not(boolean) Returns the opposite boolean value.
3 fn:true() Returns `true`.
4 fn:false() Returns `false`.

Date and Time Functions

Index Function Description
1 current-date() Returns the current date.
2 current-time() Returns the current time.
3 current-dateTime() Returns the current date and time.