views:

71

answers:

3

I believe it's possible but couldn't figure out the syntax. Something like this:

xmlNode.SelectNodes("//*[count(child::*) <= 1]")

but this is not correct.

+2  A: 

Use:

//node()[not(node())]

In case only element leaf nodes are wanted (and this needs clarification -- are elements that have non-element children considered leaf nodes?), then the following XPath expression selects them:

//*[not(*)]

Both expressions above are probably the shortest that select the desired nodes (either any-node or element -- leaf nodes).

Dimitre Novatchev
A: 

Why less or equal to 1 ?

xmlNode.SelectNodes("//*[count(child::*) = 0]")

Make tests etc at this site http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

Pretty helpful ..

Gaby
Thanks very much. This works great. So, it's more VB style equal. I thought it should be c-style because functions are case-sensitive. Why <= 1? I was confused by ChildNodes.Count which return 1 for <A>x</A>, but returns 0 for <A/>.
miliu
@Gaby and @miliu: the count test is not needed. Check @kevpie answer.
Alejandro
@Alejandro, indeed ..
Gaby
+2  A: 

Any elements with no element child

//*[not(child::*)]
kevpie
Thanks very much. It works great too.
miliu
@kevpie: +1 Right answer. But it means: *any elements with no element child*. So, it will select elements with text node child, empty elements, elements with mixed content (text nodes, PI, comments)
Alejandro
+1 @Alejandro, the clarification is appreciated!
kevpie
@kevpie: You probably meant: "Elements that don't have element-children" -- not "Elements with no children". It would be good to acknowledge this and to correct the text of your otherwise good answer.
Dimitre Novatchev
@Dimitre Thanks for holding my hand. I am SO n00b.
kevpie