【问题标题】:Using MarkLogic to counting使用 MarkLogic 进行计数
【发布时间】:2020-02-09 11:19:34
【问题描述】:

我是 MarkLogic 领域的初学者,需要帮助解决 请澄清这个问题。这是一个xml的例子 类。我需要一个函数来计算一个学生的班级数量 出席,使用地图

<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>

【问题讨论】:

  • 到目前为止你有什么尝试?

标签: nosql marklogic xquery-sql


【解决方案1】:

一种不使用地图进行计数的方法是收集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, .) )

【讨论】:

  • 谢谢你,我有一个问题你能告诉我这些怎么做吗(map:get($stats, $student), 0)[1])
  • 表达式:(map:get($stats, $student), 0)[1] 尝试从映射中获取该 $student 的当前值(第一次不会有值)和数字 0,然后使用谓词从该数字序列中选择第一个项目。如果该 $student 有一个值,它会使用它。否则,它选择数字 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多