在Xquery中传递参数 让我们考虑一个样本代码 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> }; 声明函数本地:主题(){ 让$forumUrl:=”http://www.abc.com" 对于$rootNode//h:td[@class=“alt1Active”]//h:a中的$topic 返回 {concat($forumUrl,$topic/@href',)} }; 声明函数local:thread(){ 让$forumUrl:=”http://www.abc.com" 对于$rootNode//h:td[@class=“alt2”]//h:a中的$thread 返回 {concat(forumUrl,$thread/@href',)} };

在Xquery中传递参数 让我们考虑一个样本代码 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> }; 声明函数本地:主题(){ 让$forumUrl:=”http://www.abc.com" 对于$rootNode//h:td[@class=“alt1Active”]//h:a中的$topic 返回 {concat($forumUrl,$topic/@href',)} }; 声明函数local:thread(){ 让$forumUrl:=”http://www.abc.com" 对于$rootNode//h:td[@class=“alt2”]//h:a中的$thread 返回 {concat(forumUrl,$thread/@href',)} };,xquery,Xquery,我可以在这段代码中传递任何参数而不是重复“$forumUrl”。如果可能,请帮助我。这确实是可能的,您可以传递它或将其声明为“全局”变量: 声明变量: declare variable $forumUrl := "http://www.abc.com"; declare variable $rootNode := doc('abc'); declare function local:topic(){ for $topic in $rootNode//td[@class="alt1Acti

我可以在这段代码中传递任何参数而不是重复“$forumUrl”。如果可能,请帮助我。

这确实是可能的,您可以传递它或将其声明为“全局”变量: 声明变量:

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>

};
声明变量$forumUrl:=”http://www.abc.com";
声明变量$rootNode:=doc('abc');
声明函数local:topic(){
对于$rootNode//td[@class=“alt1Active”]//a中的$topic
返回
{concat($forumUrl,$topic/@href',)}
};
声明函数local:thread(){
对于$rootNode//td[@class=“alt2”]//a中的$thread
返回
{concat($forumUrl,$thread/@href',)}
};
或者将URL作为参数传递:

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")
声明变量$rootNode:=doc('abc');
声明函数本地:主题($forumUrl){
对于$rootNode//td[@class=“alt1Active”]//a中的$topic
返回
{concat($forumUrl,$topic/@href',)}
};
声明函数本地:线程($forumUrl){
对于$rootNode//td[@class=“alt2”]//a中的$thread
返回
{concat($forumUrl,$thread/@href',)}
};
本地:主题(“http://www.abc.de")
注意:我从您的示例中删除了“h:”名称空间,并添加了
$rootNode
变量

希望这有帮助

通常,XQuery函数的参数可以指定如下:

local:foo($arg1作为类型)作为类型
其中类型可以是,例如:

  • xs:string
    a字符串
  • xs:string+
    至少一个字符串的序列
  • xs:string*
    任意数量的字符串
  • xs:string?
    长度为零的序列或字符串之一
函数可以有任意多的参数,参数的类型可以省略

函数返回值也可以键入,在示例的上下文中,签名可能是:

  • 将函数local:thread($forumUrl作为xs:string)声明为元素(thread)+
定义local:thread只接受一个字符串并返回线程元素的非空序列

迈克尔