2017-06-18 71 views
0

之间没有特定的父项我尝试选择所有具有属性名称itemprop的元素,而且whith具有属性itemtype = http://schema.org/Product的任何级别父项,但位于具有任何其他属性itemtype的节点中的元素除外。xpath:元素在最终的

实施例:

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

我只需要d和E元素,但不是A,B,C. 我试图somerhing这样的:

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

此字符串不是有效的在所有等我的尝试并未排除A,B和C元素。

回答

0

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

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

这是伟大的元素。你帮了我。非常感谢! –