目前我通过外部变量将2个数据库的名称提供给我的主xql文件。我想将这些值传递给我的任何XQL模块。
例如,我可以使用主脚本导入模块“mylib”
import module namespace mylib = "http://example.org/mylib" at "myLib.xqm";
declare variable $dbName external;
$mylib:print()
然后我用dbName外部变量提供主脚本,它可以工作,但我想将它传递给我的模块
module namespace mylib = "http://example.org/mylib";
declare variable $mylib:dbName external;
declare function mymod:print() as xs:string {
$mymod:dbName
};
如何将本地dbName的值绑定到模块的实例$ myLib:dbName ??
我试过了:
任何明显简单的解决方案?或者我是否必须静态地为任何模块定义相同的值?
答案 0 :(得分:0)
为什么不将它作为参数传递给函数?
mymod:print($dbname)
答案 1 :(得分:0)
如果您的XQuery处理器允许使用参数占位符的部分应用程序,则可以在映射中返回一组已经绑定了$dbname
参数的方法。
示例模块
xquery version "3.1";
module namespace mymod = "http://example.org/mymod";
declare function mymod:print ($dbname as xs:string) as xs:string {
$dbname
};
declare function mymod:items-by-name ($dbname as xs:string, $names as xs:string*) as xs:string {
collection($dbname)//item[@name = $names]
};
declare function mymod:get-bound-methods ($dbname as xs:string) as map(*) {
map {
(: return result of a function :)
"print": mymod:print($dbname),
(: return partially applied function:)
"items-by-name": mymod:items-by-name($dbname, ?)
}
};
用法
import module namespace mymod = "http://example.org/mymod" at "mymod.xqm";
declare variable $dbName external;
declare variable $local:bound := mymod:get-bound-methods($dbName);
(: print evaluates to a xs:string :)
$local:bound?print,
(: items-by-name evaluates to a function with an arity of 1 :)
$local:bound?items-by-name("a"),
$local:bound?items-by-name(("a", "sequence", "of", "names"))