我使用 Apache Tomcat 的 Exist DB 作为 XML 数据库,并尝试通过传递以下 xpath(在 FLWOR 的“let”子句中定义)来构造序列:
$xpath := $root/second/third
进入本地定义的函数声明,如下所示:
declare function local:someFunction($uuid as xs:string?, $xpath as xs:anyAtomicType?)
{
let $varOne := $xpath/fourth[@uuid = $uuid]/fifthRight
let $varTwo := $xpath/fourth[@uuid = $uuid]/fifthLeft
let $combined := ($varOne,$varTwo)
return $combined
};
当然,当在现有的 xquery 沙箱中输入此内容时,我得到 Type: xs:anyAtomicType is not Defined。我应该用什么来代替它,或者我应该以不同的方式来做这件事?
预先感谢您的任何建议。
最佳答案
我无法重现该错误(xs:anyAtomicType 未定义)。但是,也许以下内容可以提供帮助?
如果 $xpath (最初是一个节点)作为原子类型参数传递(因此被原子化),那么当您尝试在函数中导航时,它肯定会抛出类型错误 XPTY0019 ($xpath/fourth
)。以下代码在您这边工作吗(作为 node()*
传递)?
declare function local:someFunction($uuid as xs:string?, $xpath as node()*)
{
let $varOne := $xpath/fourth[@uuid = $uuid]/fifthRight
let $varTwo := $xpath/fourth[@uuid = $uuid]/fifthLeft
let $combined := ($varOne,$varTwo)
return $combined
};
let $root :=
<first>
<second>
<third>
<fourth uuid="1">
<fifthLeft>foo</fifthLeft>
<fifthRight>bar</fifthRight>
</fourth>
</third>
</second>
</first>
let $xpath :=$root/second/third
return
local:someFunction("1", $xpath)
(编辑:忘记星号以允许任意数量的节点)
关于xml - 如何将 xpath 传递到 xquery 函数声明中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2858224/