使用 MarkLogic 进行计数

Using MarkLogic to counting

I am a beginner in the field of MarkLogic and need help in solving this question with clarification please. This is an example of xml classes. I need a function to count the number of classes a student attends, use map

<Classes>
   <Class>
      <Class-name>math</Class-name>
      <Student-name>Jon</Student-name>
      <Student-name>Sam</Student-name>
   </Class>
   <Class>
     <Class-name>Sciences</Class-name>
     <Student-name>Jon</Student-name>
     <Student-name>Jack</Student-name>
     <Student-name>Nay</Student-name>
   </Class>
   <Class>
     <Class-name>Languages</Class-name>
     <Student-name>Jon</Student-name>
     <Student-name>Sam</Student-name>
     <Student-name>Nay</Student-name>
   </Class>
</Classes>

一种不使用地图进行计数的方法是收集不同的 Student-name 列表,然后使用这些名称来计算具有这些名称的 Student-name 元素的数量:

for $student in fn:distinct-values($Classes/Class/Student-name)
return 
  $student||":"||count($Classes/Class[Student-name=$student])

用地图实现相同目的的一种方法是遍历每个 Student-name 元素,在地图中放置一个条目,将当前计数递增 1:

let $stats := map:new()
let $_ := 
  for $student in $Classes/Class/Student-name
  return map:put($stats, $student, 1 + (map:get($stats, $student), 0)[1])
return
  map:keys($stats) ! ( .||":"||map:get($stats, .) )