How do I select the shallowest matching elements with XPath?
Posted
by harpo
on Stack Overflow
See other posts from Stack Overflow
or by harpo
Published on 2010-06-03T21:37:45Z
Indexed on
2010/06/03
21:54 UTC
Read the original article
Hit count: 330
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).
© Stack Overflow or respective owner