我想知道他们的地址中有"Lot"
的人数,来自这个使用 Xpath 的 XML 文档。
<?xml version="1.0" encoding="UTF-8"?>
<personnes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="personne.xsd">
<personne id="n1" pays="MA">
<nom>Ayoubi</nom>
<prenom>Nezard</prenom>
<dat_naiss>1999-02-05</dat_naiss>
<telephone>+21266666666</telephone>
<adress>
Lotxxx Num: 38
<ville>Rabat</ville>
</adress>
</personne>
<personne id="n11" pays="Fr">
<nom>Karimi</nom>
<prenom>Hamdani</prenom>
<dat_naiss>2000-05-07</dat_naiss>
<telephone>+21266666666</telephone>
<adress>
rue xxx Num: 18
<ville>Grenoble</ville>
</adress>
</personne>
</personnes>
我在这里测试了我的Xpaths:这里
我尝试了很多Xpath,但我不知道如何在这个Xpath上应用计数函数://adress/contains(text()[1],"Lot")
返回给我:
Boolean='true'
Boolean='false'
这个XPath,
count(//personne[adress[contains(.,'Lot')]])
将计算字符串值包含"Lot"
子字符串的adress
子元素的personne
元素的数量。 这将包括包装在其他标记中的"Lot"
子字符串。
这个XPath,
count(//personne[adress/text()[contains(.,'Lot')]])
将执行相同的操作,但排除"Lot"
其他标记中包装的子字符串。
这两个 XPath表达式都适用于 XPath 1.0 及更高版本。
参见
- 在 XPath 中测试 text() 节点与字符串值
- 为什么 XPath 包含(text(),'子字符串')不能按预期工作?
在 XPath-1.0 中,表达式必须是
contains(//adress/text()[1],"Lot")
这将为您提供true
的结果。为了得到true
和false
的结果,你必须迭代//adress/text()[1],"Lot"
节点,最好是用xsl:for-each
。
跟进,可以使用以下表达式获取包含字符串Lot
adress
子元素的personne
个元素的计数:
count(contains(//personne/adress/text()[1],"Lot"))
它的结果应该是1
.