Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments in Xquery

Tags:

xquery

let us consider a sample code

declare function local:topic(){

    let $forumUrl := "http://www.abc.com"
    for $topic in $rootNode//h:td[@class="alt1Active"]//h:a

        return
            <page>{concat($forumUrl, $topic/@href, '')}</page>

};

declare function local:thread(){

    let $forumUrl := "http://www.abc.com"
    for $thread in $rootNode//h:td[@class="alt2"]//h:a

        return
            <thread>{concat(forumUrl, $thread/@href, '')}</thread>

};

Instead of repeating "$forumUrl" , can i pass any arguments in this code.If its possible please assist me.

like image 964
Siddhu Avatar asked Jan 27 '26 10:01

Siddhu


1 Answers

this is sure possible, you may either pass it in or declare it as a "global" variable: Declared Variable:

declare variable $forumUrl := "http://www.abc.com";
declare variable $rootNode := doc('abc');
declare function local:topic(){
    for $topic in $rootNode//td[@class="alt1Active"]//a

        return
            <page>{concat($forumUrl, $topic/@href, '')}</page>

};

declare function local:thread(){


    for $thread in $rootNode//td[@class="alt2"]//a

        return
            <thread>{concat($forumUrl, $thread/@href, '')}</thread>

};

Or you pass the URL as an argument:

declare variable $rootNode := doc('abc');


declare function local:topic($forumUrl){
    for $topic in $rootNode//td[@class="alt1Active"]//a

        return
            <page>{concat($forumUrl, $topic/@href, '')}</page>

};

declare function local:thread($forumUrl){


    for $thread in $rootNode//td[@class="alt2"]//a

        return
            <thread>{concat($forumUrl, $thread/@href, '')}</thread>

};
local:topic("http://www.abc.de")

N.B. I stripped the 'h:' namespace from your example and added the $rootNode variable.

Hope this helped.

In general arguments to XQuery function may be specified as followed:

local:foo($arg1 as type) as type where type may be for example:

  • xs:string a string
  • xs:string+ a sequence of at least one string(s)
  • xs:string* an arbitrary number of strings
  • xs:string? a sequence of length zero or one of strings

Functions may have as many arguments as you wish, the type for the arguments may be omitted.

A functions return value may as well be typed, in the context of your example the signature might be:

  • declare function local:thread($forumUrl as xs:string) as element(thread)+

defining that local:thread accepts exactly one string and returns a non-empty sequence of thread elements.

Michael

like image 143
michael Avatar answered Jan 29 '26 13:01

michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!