Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an element that only has one other kind of child

Tags:

xpath

I want to use XPath to find every <blockquote> element that has at least one child <pre> element, no other kinds of child elements, and optionally text nodes as children:

<body><div><!-- arbitrary nesting -->
  <blockquote><pre>YES</pre></blockquote>
  <blockquote><p>NO</p></blockquote>
  <blockquote><pre>NO</pre><p>NO</p></blockquote>
  <blockquote><p>NO</p><pre>NO</pre></blockquote>
  <blockquote><pre>YES</pre> <pre>YES</pre></blockquote>
  <blockquote>NO</blockquote>
</div></body>

This XPath appears to work, but I suspect that it's overly complicated:

//blockquote[pre][not(*[not(name()="pre")])]

Is there a better (less code, more efficient, more DRY) way to select what I want?

like image 234
Phrogz Avatar asked Jul 11 '12 18:07

Phrogz


1 Answers

//blockquote[pre][count(pre)=count(*)]
like image 65
Chris Gerken Avatar answered Oct 31 '22 09:10

Chris Gerken