繁体   English   中英

使用 Xpath 从此 XML 文档中获取数据?

[英]Get a data from this XML doc using Xpath?

我想从使用 Xpath 的 XML 文档中知道地址中有"Lot"的人数。

<?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>

我在这里测试了我的 Xpath:这里

我尝试了很多 Xpath,但我不知道如何在此 Xpath 上应用计数 function: //adress/contains(text()[1],"Lot")返回给我:

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')]])

将计算具有字符串值包含"Lot" substring 的adress子元素的personne元素的数量。 这将包括包装在其他标记中的"Lot"子字符串。

此 XPath,

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

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

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

也可以看看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM