How to Use the XQuery Function in XML

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

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

declare function local:substract() as xs:integer

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

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

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" ?>
       <string>Dotnet Heaven</string>

Ask Your Question

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

Programming Answers here


More Articles

© 2020 DotNetHeaven. All rights reserved.