Harrison
Harrison

Reputation: 2680

XPath to select nodes which don't only have one certain child node?

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

Is there a way to select p nodes which not only have a nodes, i.e. <p>case a</p> and <p>case <a>b</a></p>, but not <p><a>case c</a></p>.

Upvotes: 1

Views: 53

Answers (1)

kjhughes
kjhughes

Reputation: 111621

This XPath,

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

will select all p elements that lack an a child or have child nodes that are not as, which is equivalent to selecting p elements which have not only a children,

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

as requested.

Upvotes: 2

Related Questions