XPath - 如何对每个元素值进行计数()和分组?

XPath - how to count() & group by each element value?

示例 XML 输入:

<?xml version="1.0"?>
<names>
  <name>abc</name>
  <name>abc</name>
  <name>xyz</name>
  <name>def</name>
  <name>ghi</name>
</names>

需要的输出为:

abc: 2
xyz: 1
def: 1
ghi: 1

试过以下:-

for $n in //names/name return concat($n, ': ', count(//$n))

但是输出是这样的:

abc: 1
abc: 1
xyz: 1
def: 1
ghi: 1

XPath 3.1 将是

map:merge(
  /names/name!map{ string() : .}, 
  map { 'duplicates': 'combine'}) 
=> 
map:for-each(function($k, $v) {
  $k || ': ' || count($v)
})

在 XPath 2 中:

for $distinct-name in distinct-values(/names/name)
return concat($distinct-name, ': ', count(/names/name[. = $distinct-name]))