2

我使用 Apache Tomcat 的 Exist DB 作为 XML 数据库,并尝试通过传递以下 xpath 来构造序列,该 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。我应该用什么来代替它,或者我应该以不同的方式来做这件事?

在此先感谢您的任何建议。

4

2 回答 2

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)

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

于 2010-05-18T15:30:16.673 回答
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

于 2010-05-19T07:05:55.600 回答