How do I select the shallowest matching elements with XPath?
- by harpo
Let's say I have this document.
<a:Root>
<a:A>
<title><a:B/></title>
<a:C>
<item><a:D/></item>
</a:C>
</a:A>
</a:Root>
And I have an XmlNode set to the <a:A> element.
If I say
A.SelectNodes( "//a:*", namespaceManager )
I get B, C, and D. But I don't want D because it's nested in another "a:" element.
If I say
A.SelectNodes( "//a:*[not(ancestor::a:*)]", namespaceManager )
of course, I get nothing, since both A and its parent are in the "a" namespace.
How can I select just B and C, that is, the shallowest children matching the namespace?
Thanks.
Note, this is XPath 1.0 (.NET 2), so I can't use in-scope-prefixes (which it appears would help).