使用 Xpath 从这个 XML 文档获取数据?

Get a data from this XML doc using Xpath?

我想知道地址中有 "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:here

我尝试了很多 Xpath,但我不知道如何在此 Xpath 上应用计数功能://adress/contains(text()[1],"Lot") 对我来说 returns:

Boolean='true'

Boolean='false'

在 XPath-1.0 中,表达式必须是

contains(//adress/text()[1],"Lot")

这将为您提供 true 的结果。要获得 truefalse 的结果,您必须遍历 //adress/text()[1],"Lot" 节点,最好使用 xsl:for-each.


接下来,您可以使用以下表达式获取其 adress 子项中包含字符串 Lotpersonne 元素的计数:

count(contains(//personne/adress/text()[1],"Lot"))

其结果应该是1

这个 XPath,

count(//personne[adress[contains(.,'Lot')]])

将计算具有 adress 个子元素且 string-value 包含 "Lot" 子字符串的 personne 元素的数量。这将包括包裹在其他标记中的 "Lot" 个子字符串。

这个 XPath,

count(//personne[adress/text()[contains(.,'Lot')]])

将做同样的事情,但排除 "Lot" 包含在其他标记中的子字符串。

两个 XPath 表达式都适用于 XPath 1.0 及更高版本。

另见