【问题标题】:Xpath how to use and statement and parent condition on a node selectionXpath如何在节点选择上使用and语句和父条件
【发布时间】:2017-12-22 22:38:34
【问题描述】:

假设我有一个这样的 XML:

<my-parent-tag attr="">
  <my-child-tag/>
</my-parent-tag>
<my-child-tag/> <!--I don't want to select this one-->

我想查找所有具有父节点 my-parent-tag 且 attr="" 的 my-child-tags。我如何在 Xpath 中表达这一点?我试过了

//my-child-tag and [../my-parent-tag[@attr='']]

但是,我最终得到了 Xpath-no-node-selected 反馈。帮助表示赞赏:)

【问题讨论】:

    标签: xml xpath


    【解决方案1】:

    为什么不直接使用正则表达式//parent/child

    从具有属性的父级中选择所有后代标签:

    //my-parent-tag[@attr='']//my-child-tag
    

    从具有属性的父级中选择所有子标记:

    //my-parent-tag[@attr='']/my-child-tag
    

    【讨论】:

    • 不是正则表达式,您的//my-child-tag 将选择所有my-child-tag 后代,而不仅仅是my-child-tag 子元素。
    【解决方案2】:

    你可以使用//my-child-tag[parent::my-parent-tag[@attr='']]

    【讨论】:

      【解决方案3】:

      这个 XPath,

      //my-parent-tag[@attr='']/my-child-tag
      

      将选择父级为my-parent-tagattr 属性值为'' 的所有my-child-tag 元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        相关资源
        最近更新 更多