【问题标题】:Get a data from this XML doc using Xpath?使用 Xpath 从这个 XML 文档中获取数据?
【发布时间】:2022-01-24 18:19:29
【问题描述】:

我想知道有多少人的地址中有"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>

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

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

Boolean='true'

Boolean='false'

【问题讨论】:

    标签: xml xpath count


    【解决方案1】:

    这个 XPath,

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

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

    这个 XPath,

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

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

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

    另见

    【讨论】:

      【解决方案2】:

      在 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

      【讨论】:

        猜你喜欢
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 2021-11-27
        • 2011-08-06
        • 1970-01-01
        相关资源
        最近更新 更多