简体   繁体   English

使用Xquery计算元素并返回元素名称和值

[英]count element using Xquery and return both the element name and value

I have to count how many times of the authors show in a XML file. 我必须数一数作者在一个XML文件中显示的次数。 Lets say,as attached - XML 附带说明-XML

Frank Manola has appeared 2 times and Tolga Yurek has appeared 1 time. 弗兰克·马诺拉(Frank Manola)出现2次,而托尔加·尤里克(Tolga Yurek)出现1次。

My desire result is: 我的愿望结果是:

Frank Manola , 2 弗兰克·马诺拉2
Tolga Yurek, 1 Tolga Yurek,1岁

I used this code, but I cannot archive it: 我使用了以下代码,但无法对其进行存档:

for $a in doc("dblp.xml")/dblp/* let$c := count ($a/author) return ($a/author , $c) 对于$ a doc(“ dblp.xml”)/ dblp / * let $ c:= count($ a / author)返回($ a / author,$ c)

Result become very weird like this: unwanted result 这样的结果变得很奇怪: 不需要的结果

What error in my code? 我的代码有什么错误? What can I group and sum them up? 我可以对它们进行分组和总结吗?

You are iterating several times on the same author. 您正在同一位作者上进行多次迭代。 Maybe you should use distinct-values when you loop on the authors? 遍历作者时,也许应该使用distinct-values?

Something like: 就像是:

for $a in distinct-values(doc("file.xml")//author)
  let $c := count(//author[. = $a])
  return ($a, $c)

In XQuery 3 you can use group by https://www.w3.org/TR/xquery-31/#id-group-by : 在XQuery 3中,您可以使用group by https://www.w3.org/TR/xquery-31/#id-group-by

for $author in doc("dblp.xml")/dblp/*/author
group by $a := $author
return $a || ': ' || count($author)

https://xqueryfiddle.liberty-development.net/b4GWVe/1 https://xqueryfiddle.liberty-development.net/b4GWVe/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM