tags:

views:

460

answers:

2

This should be an easy one but it is giving me trouble. Given this structure:

<root>
  <a>
    <b/>
  </a>
  <a/>
</root>

I'm trying to formulate an xpath expression that gives only the non-empty "a" elements, i.e. the ones that have child elements. Therefore I want the first instance of "a" returned, but not the second.

So far I have "/root/a/self::*" but that is returning me both a's.

+3  A: 
/root/a[count(*)&gt;0]

will give any 'a' node with any kind of child node

Adrian
A: 
/root/a[count(*)>0]
Torsten Marek
I think Adrian is right about the need for encoding.
Steven Sudit
Only if you want to put it into an XML file (e.g. an XSLT script). Still, that's just a storage detail, I'm pretty sure the XPath spec uses ">" :)
Torsten Marek