XQuery is a powerful language used for querying and manipulating XML data. At the heart of XQuery are its functions and operators, which allow you to perform a wide range of operations on XML documents, from simple data retrieval to complex data transformations. Understanding how to use XQuery functions and operators is essential for any XML developer because it enables efficient querying, filtering, and manipulation of XML data.
XQuery is a query language designed specifically for querying XML documents. It allows you to extract and manipulate XML data easily. XQuery is a W3C recommendation and is widely used in XML databases, web services, and applications that require XML processing.
Functions in XQuery are used to perform specific tasks, such as string manipulation, mathematical operations, or working with sequences. XQuery provides a rich set of built-in functions that you can use right away, and you can also define your own user-defined functions for more specific needs.
XQuery comes with a wide variety of built-in functions that are organized into different categories. Let’s explore some of the most commonly used built-in functions with examples.
String functions are used to manipulate and work with text in XML data.
fn:concat()
: Concatenates two or more strings.
let $title := "XQuery"
let $version := "3.1"
return fn:concat($title, " Version ", $version)
XQuery Version 3.1
fn:substring()
: Extracts a substring from a string.
let $str := "XQuery is powerful"
return fn:substring($str, 1, 6)
XQuery
Mathematical functions are used to perform arithmetic and other mathematical operations on numeric data.
fn:sum()
: Returns the sum of a sequence of numbers.
let $numbers := (10, 20, 30)
return fn:sum($numbers)
60
fn:round()
: Rounds a number to the nearest integer.
let $num := 12.56
return fn:round($num)
13
XQuery provides functions to manipulate date and time values.
fn:current-dateTime()
: Returns the current date and time.
return fn:current-dateTime()
2024-10-13T12:34:56Z
fn:year-from-date()
: Extracts the year from a date.
let $date := xs:date("2024-10-13")
return fn:year-from-date($date)
2024
Node functions are essential for working with XML nodes, such as elements, attributes, and namespaces.
fn:count()
: Returns the number of items in a sequence.
let $books :=
Book 1
Book 2
return fn:count($books/book)
2
fn:name()
: Returns the name of a node.
let $book := Learning XML
return fn:name($book)
book
In addition to built-in functions, you can define your own functions in XQuery. User-defined functions allow you to create reusable blocks of code for complex operations.
declare function local:function-name($param as data-type) as return-type {
expression
};
declare function local:add($a as xs:integer, $b as xs:integer) as xs:integer {
$a + $b
};
let $result := local:add(10, 20)
return $result
30
declare function
: Declares a function.local:add
: The function name.$a
, $b
: Parameters of type xs:integer
.$a + $b
: The expression that adds the two integers.Operators in XQuery are used to perform operations on data. XQuery supports a variety of operators, including arithmetic, comparison, and logical operators.
+
: Addition-
: Subtraction*
: Multiplicationdiv
: Division
let $a := 15
let $b := 5
return ($a + $b, $a - $b, $a * $b, $a div $b)
20 10 75 3
=
: Equality!=
: Inequality>
: Greater than<
: Less than
let $x := 10
let $y := 20
return ($x = $y, $x != $y, $x > $y, $x < $y)
false true false true
and
: Logical ANDor
: Logical ORnot()
: Logical NOT
let $a := true()
let $b := false()
return ($a and $b, $a or $b, not($a))
false true false
One of the most powerful features in XQuery is the FLWOR (For-Let-Where-Order by-Return) expression. FLWOR allows you to iterate over sequences, filter results, sort them, and return the output.
for $var in sequence
let $var2 := expression
where condition
order by expression
return result
let $books :=
XQuery Basics
Advanced XQuery
for $book in $books/book
where fn:contains($book/title, "XQuery")
return $book/title
XQuery Basics
Advanced XQuery
for $book in $books/book
: Iterates over all the book elements.where
: Filters books with “XQuery” in the title.return
: Returns the titles of the filtered books.A sequence in XQuery is an ordered collection of items (nodes or atomic values). XQuery provides functions to work with sequences, such as joining, intersecting, or returning distinct values.
fn:distinct-values()
let $numbers := (1, 2, 2, 3, 3, 3)
return fn:distinct-values($numbers)
Output:
XQuery supports conditional expressions using if-then-else
, allowing you to include logic in your queries.
if-then-else
let $age := 20
return if ($age >= 18) then "Adult" else "Minor"
Adult
We have explored XQuery functions and operators in depth, covering the most important built-in functions, user-defined functions, arithmetic, comparison, and logical operators. We have also discussed how to use FLWOR expressions and conditional logic in XQuery. By mastering these concepts, you can harness the full power of XQuery to query, filter, and manipulate XML data efficiently. Happy Coding!❤️