2016-07-18 22 views
2

給定一組xml記錄和一組術語$terms。從這組記錄中提取$terms序列中的術語。我想計算每個段落記錄中每個詞語的出現次數。我用下面的代碼可以這樣做:Xquery:計算一組記錄中每個記錄中某個詞的出現次數

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計數:

result

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

我想要的數量數學術語發生在$record/rec:paragraph

任何想法是什麼造成的是結果?有沒有其他方法可以計算每個段落中每個術語的出現次數。

+1

沒有與您的查詢幾個明顯的問題,但沒有你查詢的XML的一個例子,這是不可能肯定地說。 – wst

+1

你正在尋找'rec:paragraph'計算出的字符串值是否等於低字節'$ term'值,或者包含()'(提示,提示)這些值?此外,您可能需要小寫()「rec:paragraph」的字符串值。示例輸入和期望的結果將會有所幫助。 –

+0

我已經添加了一個例子。請看看它。 –

回答

2

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

返回此:

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

它工作。謝謝 –

相關問題