How to declare local variable in XML using XQuery

In this article we will discuss about how to declare and use the local variables in XQuery.
  • 22475

Local variable are those variables which are only accessible in the block in which they are declared. We can declare local variable in XQuery as well. Local variable can be declared  and assigned a value using the let clause in XQuery and unlike the global variables local variables can be accessed only within the scope in which they are declared.

Here is the example to declare and use the Local variables in XQuery.

declare variable $msg := ("msg1","msg2");
declare function local : print ($num as xs:integer) as item() {
           $msg[$num]
 };
<exp>
     <output1>{
                  let $x := 1;
                  return local : print($x)
          }</output1>
     <output2>{
                  let $x := 2;
                  return local : print($x)
          }</output2>
</exp>

Here $x is the local variable.

Output:

<?xml version="1.0" encoding="utf-8" ?>

<exp>

  <output1>msg1</output1>

  <output2>msg2</output2>

</exp>

Ask Your Question

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

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.