S U N D A R R A J A N

Google Search Engine

Sunday, December 13, 2009

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.

Sunday, December 6, 2009

More about XQuery

More about XQuery


Well, Hope you are following my post regularly. In this post I am doing to talk about data types in XQuery.


Before talking about data types, let me tell you who we can comment a code / have documentation about a function. In XQuery, anything starts with “(:” is considered as comment and it’s ends with “:)” (Of course)


(: This is a comment :)


Data types


As like all programming languages, XQuery also contains set of data types for example, int, float, string, double etc….


In XQuery, everything starts with namespace, look below few data types available in xquery.


xs:int
xs:string
xs:float
xs:date
xs:dateTime


In the above examples, “xs” is a namespace and int, string, float, date, dateTime are datatypes.


Now, consider we need to define array of int / float / string etc… how is that possible in XQuery?


Very simple, the following is an example for that


$variable as xs:string*


In the above example, the variable is going to hold array string; it might have 0 elements or n elements.


You might wonder, what is that big difference in the above statement to make the variable to hold array of string elements?


Well, let’s see, in xquery we have data type ends with ‘?’ and ‘*’. If the reader is familiar with XML and XSD, then it’s pretty same as that.


? – going to hold 0 or 1 elements
* - going to hold 0 or n elements


Example:


$variable as xs:string – this variable should have only one element. Please note this will not accept null / 0 element


$variable as xs:string? – this variable is going to hold 0 or 1 element. Please note this will not accept more than one element.


$variable as xs:string* – This variable is going to hold 0 or n element. Please note this will accept n number of elements so, you need to iterate it get the all the elements. 

Is this bolg useful?

Rate

S U N D A R R A J A N

Followers