【问题标题】:XPath - exclude descendants of specific selectorXPath - 排除特定选择器的后代
【发布时间】:2020-06-16 21:32:20
【问题描述】:

我有以下标记(包括 schema.org 属性):

<body>
  <div itemscope itemtype="http://schema.org/Foo">
    <div>
      <div itemname="name">
        Foo scoped name
      </div>
    </div>
    <div>
      <div itemscope itemtype="http://schema.org/Bar">
        <div>
          <div itemname="name">
            Bar scoped name
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

我需要选择(可能是通过 xpath,因为 css 选择器不足以完成任务)在 http://schema.org/Foo 范围内具有 itemname="name" 的 div,但不是那些具有 itemscope 属性提升它们的另一个元素的div。

所以在提供的示例中,我只需要选择“Foo 范围名称”,而不是“Bar 范围名称”。

【问题讨论】:

    标签: xpath


    【解决方案1】:

    你可以使用类似的东西:

    //div[@itemname="name"][ancestor::div[@itemscope][1][@itemtype="http://schema.org/Foo"]]
    

    查找具有特定属性值 (@itemname="name") 的 div 元素。它的第一个div 祖先(带有@itemscope 属性)还包含一个特定的@itemtype 属性值(http://schema.org/Foo)。

    输出:&lt;div itemname="name"&gt; Foo scoped name &lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2010-10-27
      相关资源
      最近更新 更多