gpt4 book ai didi

xml - 使用 Xquery 计算元素并返回值

转载 作者:数据小太阳 更新时间:2023-10-29 02:30:14 26 4
gpt4 key购买 nike

我的 xml 数据:

<a>
<book>
<pub>John</pub>
</book>
<book>
<pub>John</pub>
</book>
<book>
<pub>Mary</pub>
</book>
</a>

所以我想计算每个数字并显示它们

预期输出:

 <result>
<pub>John</pub>
<total>2</total>
</result>
<result>
<pub>Mary</pub>
<total>1</total>
</result>

但是我的输出:

 <result>
<pub>John</pub>
<total>1</total>
</result>
<result>
<pub>John</pub>
<total>1</total>
</result>
<result>
<pub>Mary</pub>
<total>1</total>
</result>

我使用的代码:

for $b in /a/book
let $count := count($b/pub)
for $pub in $b/pub
return
<result> {$pub} <total>{$count}</total></result>

它不断循环相同的数据但不对其进行分组。即使我使用不同的值,它仍然是一样的。我的代码有什么错误?

最佳答案

如果使用支持 XQuery 3.0 的 XQuery 处理器,您还可以利用 group by flwor 语句:

for $book in /a/book
let $publisher := $book/pub
group by $publisher
return
<result>
<pub>{ $publisher }</pub>
<count>{ count($book) }</count>
</result>

关于xml - 使用 Xquery 计算元素并返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107842/

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