XQuery (DB2) 替换节点(如果存在)。有可能吗?

发布于 2024-11-18 02:11:57 字数 1170 浏览 2 评论 0原文

请参阅以下查询:

select
  xmlquery
  (
    '
      if ($xml/foo/bar) then
        transform copy $xml := $xml modify do replace $xml/foo/bar with <bar>some stuff</bar> return $xml
      else
        $xml
    '
    passing xmlparse(document '<foo><bar>something here</bar></foo>') as "xml"
  )
from sysibm.sysdummy1

此查询工作得很好:它替换了 /foo/bar 节点,我非常高兴。如果 /foo/bar 节点不存在,我只想什么都不会发生。所以下面的查询应该只返回未修改的 XML:

select
  xmlquery
  (
    '
      if ($xml/foo/bar) then
        transform copy $xml := $xml modify do replace $xml/foo/bar with <bar>some stuff</bar> return $xml
      else
        $xml
    '
    passing xmlparse(document '<foo>nothing here</foo>') as "xml"
  )
from sysibm.sysdummy1

嗯...所以我想。事实上 DB2 高兴地告诉我:

SQL16085N XQuery“替换”表达式的目标节点无效。

也许我对 if 的语法是错误的,并且“then”部分在上面的两个查询中都执行了。所以我尝试交换 then 和 else,因为显然 DB2 不应该尝试执行 then 和 else 部分。但这根本没有改变任何事情。

现在我显然很困惑。是不是不可能做我喜欢用 XPath 做的事情,因为所有路径表达式无论执行与否都必须满足?或者我只是做错了什么?

See the following query:

select
  xmlquery
  (
    '
      if ($xml/foo/bar) then
        transform copy $xml := $xml modify do replace $xml/foo/bar with <bar>some stuff</bar> return $xml
      else
        $xml
    '
    passing xmlparse(document '<foo><bar>something here</bar></foo>') as "xml"
  )
from sysibm.sysdummy1

This query works just fine: It replaces the /foo/bar node and I am perfectly happy. In case that the /foo/bar node does not exist I just want nothing to happen. So the following query should just return the unmodified XML:

select
  xmlquery
  (
    '
      if ($xml/foo/bar) then
        transform copy $xml := $xml modify do replace $xml/foo/bar with <bar>some stuff</bar> return $xml
      else
        $xml
    '
    passing xmlparse(document '<foo>nothing here</foo>') as "xml"
  )
from sysibm.sysdummy1

Well... so I thought. In fact DB2 happily tells me:

SQL16085N The target node of an XQuery "replace" expression is not valid.

Maybe I am wrong about the syntax of the if I thought and the "then"-part is executed in both the queries above. So I tried to swap the then and the else, because clearly DB2 should not attempt to execute the then AND the else part. But that did not change anything at all.

Now I am clearly confused. Is it not possible to do what I like to do with XPath, because all path expressions must be satisfied whether they are executed or not? Or am I just doing something wrong?

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

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

发布评论

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

评论(2

百思不得你姐 2024-11-25 02:11:57

我有类似的问题,我解决了它:

select
    xmlquery('
        let $xml0 := <xml><foo><bar/></foo></xml> return
        if ($xml/foo/bar) then
            transform copy $xml := ($xml[foo/bar], $xml0)[1] modify 
            do replace $xml/foo/bar with <bar>some stuff</bar>
            return $xml
        else
            $xml
            ' passing xmlparse(document '<foo>nothing here</foo>') as "xml")
            from sysibm.sysdummy1

这似乎是一个错误

I had similar problem and I solved it so:

select
    xmlquery('
        let $xml0 := <xml><foo><bar/></foo></xml> return
        if ($xml/foo/bar) then
            transform copy $xml := ($xml[foo/bar], $xml0)[1] modify 
            do replace $xml/foo/bar with <bar>some stuff</bar>
            return $xml
        else
            $xml
            ' passing xmlparse(document '<foo>nothing here</foo>') as "xml")
            from sysibm.sysdummy1

It seems like a bug

╰沐子 2024-11-25 02:11:57

并且您不能使用任何类型的函数“exists(....)”,例如 fn:exists(item1, item2...) 其中,如果参数的值不是空序列,则存在函数返回 true,否则它返回假?
当然我对 db2 一无所知:)

And counln't you use any type of function "exists(....)" for example fn:exists(item1, item2...) where exists function returns true if the value of the arguments IS NOT an empty sequence, otherwise it returns false?
Granted that I know nothing about db2 :)

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