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

标签 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 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/

相关文章:

xml - 从 xml 属性查找并执行数学表达式并替换值

Android TextView 断行不正确

xml - Google 表格的 IMPORTXML XPath_Query

regex - 在 XSLT/XQuery 正则表达式中,对交替表达式的求值是否有要求的顺序?

xml - 如何在 XQuery 中打开字符串?

android - backgroundTint 对 Lollipop 版本没有影响

java - Spring:JSP 未读取传递的 ModelMap?

python-3.x - 尝试遍历 text() 节点以找到它们的父节点

xpath - Selenium WebDriver findElement(By.xpath()) 不起作用

ios - Swift xpath 搜索返回字符串