Home

>

Tutorials

>

Get Started With XQuery

>

XQuery: Local Variables

 
 

XQuery: Local Variables

Introduction

Local variables can be declared in different location within an XQuery module. Local variables are only accessible in a limited scope. For example, they are only visible within a function body or wihtin a return clause (the return expression will be explained more in detail in the FLWOR explanations).

Example

So far we have added a global variable and a function declaration to our example. This already contains a local variable $number that is used as function parameter and is only accessible from within the function body. Now, add a local variable that is only accessible within following expressions and the visibility will last until the return expression ends:
XQuery
declare variable $hello := ("Hello World1","Hello World2");

declare function local:message ($number as xs:integer) as item() {
    $hello[$number]
};

<test>
    <message1>{ 
        (: local variable declaration :)
        let $idx := 1
        return local:message($idx)
    }</message1>
    <message2>{ 
        (: another local variable declaration :)
        let $idx := 2
        return local:message($idx) 
    }</message2>
</test>
                    
Declaration of a local variable using a let expression.
The result is again:
XML Result
<?xml version="1.0" encoding="UTF-8"?>
<test>
    <message1>Hello World1</message1>
    <message2>Hello World2</message2>
</test>
                    
Result

Plenty More XQuery Examples: Local Variables

Here you can find some more examples for local variable declarations.

XQuery Local Variables: Using Multiple Local Variables

Local variables assigned with a let expression can eventually be used in following let expressions to define more variables.
XQuery
<result>{
    let $x := 1
    let $y := "some other stuff"
    let $z := $x + 1
    return 
        (
            <x>{$x}</x>,
            <y>{$y}</y>,
            <y>{$z}</y>
        )
}</result>
                                
XQuery Local Variables: Using Multiple Local Variables
XML Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <x>1</x>
  <y>some other stuff</y>
  <y>2</y>
</result>
                                
Result

XQuery Local Variables: Typing And Quantifying Local Variables

Just like globale variable local variables can optionally be typed and quantified ('*', '+', '?', ' '). Here are some examples:
XQuery
<result>{
    let $empty as empty-sequence() := ()
    let $r as item()* := ("mixed",<content />)
    let $s as xs:anyAtomicType+ := ( 1, 2.5, "a string" )
    let $t as xs:integer? := 1
    let $v as xs:string := "some other stuff"
    let $w as element(test) := <test>hello</test>
    let $x as element()+ := ( <only />,<elements />)
    return 
        (
            <empty>{$empty}</empty>,
            <r>{$r}</r>,
            <s>{$s}</s>,
            <u>{$t}</u>,
            <v>{$v}</v>,
            <w>{$w}</w>,
            <x>{$x}</x>
        )
}</result>
                                
XQuery Local Variables: Typing And Quantifying Local Variables
XML Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <empty/>
  <r>mixed<content/>
  </r>
  <s>1 2.5 a string</s>
  <u>1</u>
  <v>some other stuff</v>
  <w>
    <test>hello</test>
  </w>
  <x>
    <only/>
    <elements/>
  </x>
</result>
                                
Result

XQuery Local Variables: Redefining Local Variables

Local variables assigned with a let expression can eventually be redefined in nested expressions without affecting the outer declarations:
XQuery
<result>{
    (: outer $x :)
    let $x := 1
    let $y := 
    
        (:  
            - a nested expression
            - the result will be assigned to $y
            - the inner declaration of $x does not affect the outer one
            - the inner declaration of $x is only accessible wihtin the nested expression
        :)
        (: new inner $x defined using the outer $x :)
        let $x := $x + 1 
        return
            (: inner $x gets returned -> 2 :) 
            $x
    return 
        (
            (: outer $x was not changed by inner $x -> it's still 1 :)
            <x>{$x}</x>,
            <y>{$y}</y>
        )
}</result>
                                
XQuery Local Variables: Redefining Local Variables
XML Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <x>1</x>
  <y>2</y>
</result>                                
Result
 
blog comments powered by Disqus