How to Use the XQuery Function in XML

In this article we will discuss about how to make and use the functions in XQuery.
  • 1885

There is a facility in XQuery that we can make our own functions and these kinds of functions are called user-Defined functions. Instead there are approx 100 built-in-functions available in XQuery but if we donot have the need of such functions then we can make our own functions as well. Every function must have prefix. The syntax of declaring a User-Defined function is as follows.

declare function prefix:function_name($Parameter as Datatype)
As returndatatype
{
function body......
}

In the above syntax of declaring the User-defined function we need to use the declare keyword while declaring the function and the function name must have the prefix along with its name. Here is the example of using the function in XQuery.

declare function local:message() as xs:string
{
    "Dotnet Heaven"
};

declare function local:sum() as xs:integer
{
    3+4
};

declare function local:substract() as xs:integer
{
   6-5
};

declare function local:division() as xs:double
{
    xs:double( 2 div 3)
}

<output>
      <string>{ local: message() }</string>
      <integer>{ local: sum() }</integer>
      <integer>{ local: substract() }</integer>
      <double>{ local: division() }</double>
</output>       

In the above statement a call to the functions have been made. Like    <string>{ local: message() }</string> .

The output of the above function will be:

<?xml version="1.0" encoding="utf-8" ?>
 <output>
       <string>Dotnet Heaven</string>
       <integer>7</integer>
       <integer>1</integer>
       <double>0.6666666666666666</double>
 </output>

Ask Your Question

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.