Functions in XQuery
In this week let’s talk about the functions in the xquery.
As I described in my previous posts, all the xquery variables and functions starts with namespace. If you are familiar with XSD and XSL, then this is pretty much same. As like other programming languages, xquery is also having predefined functions.
Before going to the user defined function, let’s talk about predefined functions.
XQuery has 2 namespaces, fn and fn-bea.
Example:
fn:string() – this function accepts any atomic type and converts it to string.
fn:number() – this function accepts any atomic type and converts it to number.
fn-bea:trim() – this function accepts string argument and returns string after trimming it.
fn-bea:date-from-dateTime() – this function accepts dateTime as argument and returns date after converting it to date.
Developers can also define their own function depends on the business needs.
Find below the example for the user defined functions
declare function tns:getValue($arg0 as xs:string?) as xs:string* {
(: your business logic :)
};
The above is an example for user defined function, in the above function getValue accepts string argument which can be 0 or 1 and returns string which can be 0 or n elements.