如何将 xpath 传递到 xquery 函数声明中

发布于 2024-09-02 04:01:52 字数 562 浏览 4 评论 0原文

我使用 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。我应该用什么来代替它,或者我应该以不同的方式来做这件事?

预先感谢您的任何建议。

I use Apache Tomcat's Exist DB as an XML database and am trying to construct a sequence by passing the following xpath, defined in FLWOR's 'let' clause:

$xpath := $root/second/third

into a locally defined function declaration, like so:

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
};

Of course, when entering this in the exist xquery sandbox, I get Type: xs:anyAtomicType is not defined. What should I use in place of it, or should I do this a different way?

Thanks in advance for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-09-09 04:01:52

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

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

I could not reproduce the error (xs:anyAtomicType not defined). However, maybe the following can help?

If $xpath (initially a node) is passed as an atomic-type parameter (thus is atomized), it will definitely throw a type error XPTY0019 when you attempt to navigate in your function ($xpath/fourth). Does the following code work on your side (passed as node()* instead)?

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)

(Edit: forgot the star to allow any number of nodes)

岁月染过的梦 2024-09-09 04:01:52

像 $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 是一个独立的开源项目,不是连接到 Apache 或 Tomcat,尽管它使用 Xalan 和 Lucene 等 Apache 组件

An expression like $root/second/third in general yields a sequence of items so it's not helpful to think of it as a path. Using the type xs:anyAtomicType? will coerce the items to a atom.

You can simplify the function to

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

BTW eXist db is an independent open source project not connected to Apache or Tomcat although it uses Apache components like Xalan and Lucene

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文