Xquery:计算一组记录中每个记录中某个术语的出现次数

Xquery: Counting the number of occurrences of a term in each record within a set of records

给定一组 xml 记录和一组术语 $terms$terms 序列中的术语是从记录集中提取的。我想计算每个段落记录中每个术语的出现次数。我使用了以下代码来做到这一点:

for $record in /rec:Record
for $term in $terms
return   xdmp:unquote(concat('<info>',string(count(lower-case($record/rec:paragraph )[. = lower-case($term)])), '</info>'))

对于每条记录中的每个术语,我得到 0 个计数:

示例:$term:='Mathematics'$record/rec:paragraph:='Mathematics is the study of topics such as quantity'

我想要数学一词在 $record/rec:paragraph

中出现的次数

知道是什么导致了这个结果吗?有没有其他方法可以计算每个段落中每个术语的出现次数。

使用 tokenize() 将输入字符串拆分为单词标记。那么计数本身就是微不足道的。例如:

let $text := 'Mathematics is the study of topics such as quantity'
let $myterms := 'mathematics'
let $wds := tokenize($text, '\s+')

for $t in $myterms
return <term name="{$t}">{count($wds[lower-case(.)=lower-case($t)])}</term>

Returns这个:

<term nm="mathematics">1</term>