![](/img/trans.png)
[英]Count the number of elements are matching with for the given xpath expression
[英]What expression to use with XQuery to count the number of matching strings?
我正在嘗試計算包含某個單詞的節點數。 如何計算總匹配數?
<Fruit>
<type>apple</type>
<type>orange</type>
<type>apple</type>
<type>apple</type>
</Fruit>
因此,蘋果的數量為3。如何獲得它。 我已經能夠獲取信息,並且在標准化之后我可以打印出:
<text>apple orange apple apple</text>
let $a := <Fruit>
<type>apple</type>
<type>orange</type>
<type>apple</type>
<type>apple</type>
</Fruit>
return
count($a/type[text() eq 'apple'])
應該帶你去
要計算具有精確值的元素,請使用eq
:
count(/Fruit/type[. eq 'apple'])
要計算包含特定子字符串的元素,請使用contains()
:
count(/Fruit/type[contains(.,'apple')])
因此,其中還包括<type>this is an apple</type>
。
對於常規單詞搜索,您需要XQuery 1.0以外的功能。 例如,如果您使用的是MarkLogic,則可以執行以下操作:
count(/Fruit/type[cts:contains(.,'apple')])
也可以匹配<type>Apples</type>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.