This is the structure of my XML file -
James Bond
Allen Bond
XQuery 3.0 solution with group by:
<A>{
for $name in //Name
let $full :=
if(not($name/@p)) then concat($name, ' ', $name/@t)
else concat($name, ' ', $name/@t, ', ', $name/@p)
group by $full
return <N>{$full, '-', count($name)}</N>
}</A>
XQuery 1.0 solution with distinct-values($arg):
<A>{
let $names :=
for $name in //Name
return if(not($name/@p)) then concat($name, ' ', $name/@t)
else concat($name, ' ', $name/@t, ', ', $name/@p)
for $name in distinct-values($names)
return <N>{$name, '-', count($names[. eq $name])}</N>
}</A>