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 positive is being returned by function. In case we input positive value we should get positive value as a output again.
abs(1.25) = 1.25.
2. fn:ceiling(3.14)= 4, This function returns 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.
3. fn:floor(3.14)= 3, returns greatest integer lesser than number passed. This works similar to 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.
4. fn:string-length($any_string as xs:string) as xs:integer, This function returns 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 parameter to this function.
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 unction 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.
6. fn:false(): This function returns false.
7. fn:true(): This function returns true.
8. fn:not(argument): This function returns the reverse of argument passed in boolean format.
9. Current-date(): This function return the current date as per the system date configured.
10. Current-time(): This function return the current time as per the system time configured.
11. Current-datetime(): This function return the current date and time as per the system configurations.
12. fn:error(), Used to return the error as and when required.
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 description of 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.
We can pass value and its label as parameter to this function while debugging a session.
17. fn:node-name(node), This function is used to return the value of node name for the argument node.
18. fn:document-uri(node): For the passed node, document-uri is picked up and provided as 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 result of this function.
21. fn:nilled(node): returns boolean value indicating if node is nil or not.
22. fn:round-half-to-even(number): Rounds up the number to reach nearest even number.
23. fn:return(), This function is used to return output of any function. We can pass parameter via it.
24. fn:round(3.14)= 3, This function rounds up the value to nearest integer. If the value after 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 nearest small integer number. For ex: round(4.6)=5.
25. fn: concat($input as xs:anyatomictype?) as xs:string: This function takes two or more strings and concatenate them to output single concatenated string. This function works to take input as string and returns an output in string format only.
concat($input_string as xs:anyatomictype?) as xs:string
XQuery is one of the standard data extraction functional programming logic. It is powerful tool to display or update the 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.
This is a guide to XQuery Functions. Here we discuss the Introduction to XQuery Functions and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –