如何在 MarkLogic 数据库中获取 XML 中的节点总数

How to get total number of nodes in an XML in MarkLogic Database

我的数据库中有如下 XML:

<PersonalData>
  <Person>
    <Name></Name>
    <Age></Age>
    <AccountNo>
      <Number>123<Number>
      <SwiftCode>1235<SwiftCode>
    </AccountNo>
    <AccountNo>
      <Number>15523<Number>
      <SwiftCode>188235<SwiftCode>
    </AccountNo>
  </Person>
</PersonalData>

在这个 XML 中,我有多个 AccountNo 节点,我的数据库中有大约 1M 的相似记录。我想确定整个数据库中 AccountNo 个节点的数量。

报告 AccountNo 元素计数的一种方法是使用 XPath 和计数:

count(//AccountNo)

您也可以使用 cts:search 并在 $expression XPath 中指定 AccountNo,然后 count() 结果:

count(cts:search(//AccountNo, cts:true-query()))

另一种计算所有不同 AccountNo 元素的方法是 运行 一个 CoRB job to select the docs that have those elements, and then in the process module return a line for every element in the doc and write the results to a text file. Below is an example OPTIONS-FILE 可用于实现该目的:

URIS-MODULE=INLINE-XQUERY|let $uris := cts:uris('',(),cts:element-query(xs:QName("AccountNo"), cts:true-query())) return (count($uris), $uris)
PROCESS-MODULE=INLINE-XQUERY|declare variable $URI external; doc($URI)//AccountNo ! 1
PROCESS-TASK=com.marklogic.developer.corb.ExportBatchToFileTask
EXPORT-FILE-NAME=AccountNoCounts.txt
DISK-QUEUE=true

然后你可以从结果文件中得到行数,这会告诉你有很多元素:wc -l AccountNoCounts.txt

如果您需要能够经常获得此计数,并且需要快速响应,您可以创建一个 TDE that projects rows for each of the AccountNo elements and then could and could select the count with SQL (e.g. SELECT count(1) FROM Person.AccountNo) or use the Optic API against that TDE and op.count().