2017-06-18 72 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

這是偉大的元素。你幫了我。非常感謝! –