gpt4 book ai didi

xquery - 函数实现 "Element does not have this child"?

转载 作者:行者123 更新时间:2023-12-04 07:05:11 25 4
gpt4 key购买 nike

我想检索集合中满足给定元素没有特定子元素的文档。例如,从下面的两个“文档”中,我想选择 doc 2,因为它有元素 child,但我想选择 doc 1,因为它没有。

文档 1:

<doc>
<element>
<child/>
</element>
</doc>

文档 2:

<doc>
<element>
<other-child/>
</element>
</doc>

我试过这样做:

for $item in collection('/db/collection')
where not($item//child)
return $item

但是,由于某些原因,这不起作用。我还尝试了 exists 的各种组合、序列函数等,但无法获得我想要的结果。

需要澄清的是,not($item//child) 测试似乎并没有按照我的预期进行。上面的查询返回我集合中的每个文档,无论它是否有元素。然而,反之亦然:

for $item in collection('/db/collection')
where $item//child
return $item

此查询准确返回那些具有子元素的文档。

什么给了?

最佳答案

我认为您需要同时使用 not() 和 exists() 来实现您想要的结果。所以,我想你想做这样的事情:

for $item in collection('/db/collection') return
if (not(exists($item//child))) then $item
else ()

关于您的 namespace 问题:如果您的文档使用 namespace ,那么您的 XQuery 语句中应该有正确的 namespace 声明,例如 declare namespace ns="some_uri";

希望这对您有所帮助。
-tjw

关于xquery - 函数实现 "Element does not have this child"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1211153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com