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.