XPath选择不仅有一个特定子节点的节点?

问题描述 投票:1回答:1
<div>
  <p>case a</p> # only has a text node, selected
  <p>case <a>b</a></p> # has a text node and an a node, selected
  <p><a>case c</a></p> # only has an a node, not selected
</div>

有没有办法选择不仅有p节点的a节点,即<p>case a</p><p>case <a>b</a></p>,而不是<p><a>case c</a></p>

html xml xpath
1个回答
2
投票

这个XPath,

//p[not(a) or node()[not(self::a)]]

将选择缺少p孩子的所有a元素或具有不是as的子节点,这相当于选择不仅有p孩子的a元素,

<p>case a</p>
<p>case <a>b</a></p>

按照要求。

© www.soinside.com 2019 - 2024. All rights reserved.