如何将xpath传递给xquery函数声明

时间:2010-05-18 14:35:54

标签: xml xpath xquery exist-db

我使用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未定义。我应该用什么代替它,或者我应该以不同的方式做到这一点?

提前感谢任何建议。

2 个答案:

答案 0 :(得分:1)

我无法重现错误(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)

(编辑:忘记了星号以允许任意数量的节点)

答案 1 :(得分:0)

$ root / second / third这样的表达式通常会生成一系列项目,因此将其视为路径没有帮助。使用类型xs:anyAtomicType?将物品强制转换为原子。

您可以将功能简化为

declare function local:y($uuid as xs:string?, $xpath as item()*)
{
  $xpath/fourth[@uuid = $uuid]/(fifthRight,fifthLeft)
};

BTW eXist db是一个独立的开源项目,虽然它使用像Xalan和Lucene这样的Apache组件,却没有连接到Apache或Tomcat