(XPATH)限制“祖先”的范围

时间:2019-12-06 16:47:04

标签: xml xpath

我可以限制“祖先”的范围吗? 例如,

1...<div itemtype="product" itemprop="name">
2... <div itemtype="product">
3...   <div itemtype="List"> 
4...     <span itemprop="name"> </span>
5...  </div>
6...   <span itemprop="name"> </c>
7... </div>
8...</div>

条件: 1.选择节点* [@@ itemtype =“ product',而不是(@itemprop)] 2.在该子节点下,选择itemprop,但在它们之间没有新的项目类型,在这种情况下,只应选择第6行。

I used this code, but not working due to ancestor nodes
//*[@itemtype and not(@itemprop) and contains(@itemtype, '/Product')]//*[@itemprop='name' and count(ancestor::*[contains(@itemtype, 'schema.org/Product')])=count(ancestor::*[@itemtype])]

如何从祖先搜索中排除顶部节点?

1...<div itemtype="product" itemprop="name"> --> ignore this line
-------------------------------------------> 
2... <div itemtype="product">
3...   <div itemtype="List"> 
4...     <span itemprop="name"> </span>
5...  </div>
6...   <span itemprop="name"> </c>
7... </div>
8...</div>

1 个答案:

答案 0 :(得分:1)

如果我对您的理解正确,那么您正在寻找这样的东西:

//*[@itemtype='product'][not(@itemprop='name')]/descendant-or-self::*[1]

输出:

 <div itemtype="product">
   <div itemtype="List"> 
      <span itemprop="name"> </span>
   </div>
   <span itemprop="name"> </span>
</div>