(XPATH)限制"Ancestor"的范围



我可以限制"祖先"的范围吗? 例如

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="产品"而不是(@itemprop(] 2. 在此子节点下,选择 itemprop,但在它们之间,没有新的 itemtype,在这种情况下,只应选择第 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>

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

//*[@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>

最新更新