Hello Markus,
In your query, you're grouping only by $count but also returning $incidence. When you group by $count, you're creating a sequence of unique count values each associated with a sequence of $incidences. To illustrate:
<contributors>{
for $x in $results
let $count := $x(1)
let $incidence := count($count)
group by $count
return <contributor count="{$count}">{
for $i in $incidence
return <incidence>{$i}</incidence>
}</contributor>
}</contributors>
Will result in:
<contributors>
<contributor count="1">
<incidence>1</incidence>
<incidence>1</incidence>
<incidence>1</incidence>
</contributor>
<contributor count="2">
<incidence>1</incidence>
</contributor>
</contributors>
So when you attempt to print out
<incidence>{$incidence}</incidence>, XQuery finds a sequence (1, 1, 1) instead of the value you want, 3.
To get the count of $incidence, you could return <incidence>{count($incidence)}</incidence> instead. But since you're not using the IDs at all, placing them in a sequence of arrays first isn't necessary. You can skip that part like:
for $x in //test_data/product
let $count := count($x/contributor)
let $id := $x/id/data()
group by $count
let $incidence := count($id)
order by $incidence descending
return
<csv>
<record>
<contributor_count>{$count}</contributor_count>
<incidence>{$incidence}</incidence>
</record>
</csv>
-Tamara