xpath:元素在最终元素之间没有特定的父元素 [英] xpath: element have not specific parent between final

查看:30
本文介绍了xpath:元素在最终元素之间没有特定的父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试选择所有具有属性名称 itemprop 并且具有属性 itemtype = http://schema.org/Product 的任何级别父级的元素,除了元素位于具有任何其他属性 itemtype 的节点中.

示例:

<div itemprop = "name" >D</div><div><div><div itemprop = "价格" ></div>

我只需要 D 和 E 元素而不需要 A、B、C.我试过这样的:

//*[normalize-space(@itemtype) = 'http://schema.org/Product']/descendant::*[not(descendant-or-self::*[@itemtype]])][@itemprop]

这个字符串根本无效,我的其他尝试没有排除 A、B 和 C 元素.

解决方案

尝试使用下面的表达式,它允许获取具有 itemtype"http://schema 属性的祖先元素.org/Product" 仅值

//div[@itemprop and count(ancestor::*[@itemtype='http://schema.org/Product'])=count(ancestor::*[@itemtype])]

I try to select all elements which has attribute name itemprop and whith has any level parent with attribute itemtype = http://schema.org/Product except elements which located into nodes with any other attribute itemtype.

Example:

<div itemtype = "http://schema.org/Product" >
  <div itemtype = "http://schema.org/BreadcrumbList" >
    <div itemprop = "name" > A </div>
    <div itemprop = "price" > B </div>
    <div itemtype = "http://schema.org/ListItem" >
      <div itemprop = "description"> C </div>
    </div>
  </div>
  <div itemprop = "name" > D </div>
  <div>
    <div>
      <div itemprop = "price" > E </div>
    </div>
  </div>
</div>

I need only D and E elements but not A, B, C. I tried somerhing like this:

//*[normalize-space(@itemtype) = 'http://schema.org/Product']/descendant::*[not(descendant-or-self::*[@itemtype])][@itemprop]

This string not valid at all and other my attempts did not exclude A, B and C elements.

解决方案

Try to use below expression which allow to get elements with ancestors that have attribute itemtype with "http://schema.org/Product" value only

//div[@itemprop and count(ancestor::*[@itemtype='http://schema.org/Product'])=count(ancestor::*[@itemtype])]

这篇关于xpath:元素在最终元素之间没有特定的父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆