Updated April 1, 2023
Introduction to XQuery Functions
XQuery is the functional programming that contains functions to leverage the power of data by extracting data from XML Documents. Xquery uses XPath as a base to design its query. Xquery version 1.0 shares its data model with Xpath 2.0. So all the functions are supported both of these languages. There are many built-in functions in XQuery language sourced from predefined libraries.
These libraries should be included in the project prior to use of functions so that they get a reference to reach their definition. Along with the built-in functions, we can define our own customized functions. These customized functions can then be used later in the program as per business requirements.
XQuery Built-in Functions
Customized XQuery functions are made up of data members, operators and in-built functions.
In this topic, we are going to understand XQuery built in functions only.
These can be majorly grouped as:
- Node Functions
- Numeric Functions
- Error and Trace Functions
- String Functions
- Duration/Date/Time Functions
- Boolean Functions
- Accessor Functions
- AnyURI Functions
Below functions explained are part of the groups mentioned above:
1. fn:abs (-3.45)= 3.45, This function returns the absolute value of input numeric. This function is used to get the positive value irrespective of any signed valued inputted in the function. In this case, the negative value is being entered and the positive is being returned by the function. In case we input a positive value we should get a positive value as output again.
Example:
abs(1.25) = 1.25.
Syntax:
fn:abs(number)
2. fn:ceiling(3.14)= 4, This function returns the smallest integer greater than the number passed. In the above example we irrespective of any value we put which has values in decimal format, we get the nearest high integer. In this case 3.14 is nearest to 4 integer.
Syntax:
fn:ceiling(number)
3. fn:floor(3.14)= 3, returns greatest integer lesser than number passed. This works similar to the previous function but the difference is that it converts any decimal value to the nearest lower integer value. In this case 3.14 is converted into 3 as it is the nearest lower integer. Such functions are required when we want to have only integer values in our program.
Syntax:
fn:floor(number)
4. fn:string-length($any_string as xs:string) as xs:integer, This function returns the number of characters in the string specified. So we can pass any string into this function and get the number of characters inputted in the string. This is useful function when we want to iterate through the string characters as per our program requirements. For example: string-length($WhatIsTheLengthOfThisString as xs:string) as xs:integer. This function will return an integer value 27. 27 is the number of characters passed as a parameter to this function.
Syntax:
string-length($input_string as xs:string) as xs:integer
5. fn:boolean(5)=True, This function returns the Boolean value. If there is any number then it returns true otherwise false. This function returns true or false based on any input number, string or node. This function just determines if there exists any object or not. For ex: boolean(23), gthis function returns true as it has some value. We can pass 23 as string or number after storing it as a variable.
Syntax:
fn:boolean(argument)
6. fn:false(): This function returns false.
7. fn:true(): This function returns true.
8. fn:not(argument): This function returns the reverse of the argument passed in boolean format.
9. Current-date(): This function returns the current date as per the system date configured.
Syntax:
fn:current-date()
10. Current-time(): This function returns the current time as per the system time configured.
Syntax:
fn:current-time()
11. Current-datetime(): This function returns the current date and time as per the system configurations.
Syntax:
fn:current-datetime()
12. fn:error(), Used to return the error as and when required.
Syntax:
fn:error()
13. fn:error(error,description,error-object): This is to catch or display the error providing error, description, and error object as a parameter.
14. fn:error(error,description): Using “description” parameter we can pass the description of the error itself. This will help the end user comprehend the error better.
15. fn:error(error): This is to catch or display the error providing error as a parameter.
16. Fn:trace(value,label), This function is used while debugging the queries.
Syntax:
fn:trace(value,label) //
We can pass the value and its label as parameters to this function while debugging a session.
17. fn:node-name(node), This function is used to return the value of the node name for the argument node.
Syntax:
fn:node-name(node)
18. fn:document-uri(node): For the passed node, document-uri is picked up and provided as a result of this function.
19. fn:base-uri(): For the passed node, base-uri is picked up.
20. fn:base-uri(node): For the passed node, base-uri is picked up and provided as a result of this function.
21. fn:nilled(node): Returns boolean value indicating if the node is nil or not.
22. fn:round-half-to-even(number): Rounds up the number to reach the nearest even number.
23. fn:return(), This function is used to return the output of any function. We can pass parameters via it.
Syntax:
fn:return()
24. fn:round(3.14)= 3, This function rounds up the value to the nearest integer. If the value after the decimal is 5 or more then the decimal value is rounded up to the nearest high integer value while for decimal numbers less than 5, they are rounded to the nearest small integer number. For ex: round(4.6)=5.
Syntax:
fn:round(number)
25. fn: concat($input as xs:anyatomictype?) as xs:string: This function takes two or more strings and concatenates them to output a single concatenated string. This function works to take input as a string and returns output in string format only.
Syntax:
concat($input_string as xs:anyatomictype?) as xs:string
Conclusion
XQuery is one of the standard data extraction functional programming logic. It is a powerful tool to display or update data on the go. In this era of connectivity where XML documents are being passed with other applications establishing a data linkage. Various functions are used either in-built or customized to meet the requirements. It follows a basic syntax as other programming languages.
Recommended Articles
This is a guide to XQuery Functions. Here we discuss the introduction and XQuery built-in functions for better understanding. You can also go through our suggested articles to learn more –