XQuery (DB2) 替换节点(如果存在)。有可能吗?
请参阅以下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有类似的问题,我解决了它:
这似乎是一个错误
I had similar problem and I solved it so:
It seems like a bug
并且您不能使用任何类型的函数“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 :)