2

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).

Also, this isn't really a question about namespaces. The quandary would be the same with other matching criteria.

harpo
  • 41,820
  • 13
  • 96
  • 131

3 Answers3

2

What about this:

<xsl:variable name="parents" select="ancestor-or-self::a:*" />
<xsl:value-of select="//a:*[not(deep-equal(ancestor::a:*, $parent))]" />

In XSLT this seems simple to do (store a node set as a variable), but I don't exactly know how to implement this in C#.

Edit: Working further on the idea of using count, this can probably work:

int nrParents = A.SelectNodes("ancestor-or-self::a:*", namespaceManager).Count(); // Or was it Size?
A.SelectNodes("//a:*[count(ancestor::a:*)!=" + nrParents + "]", namespaceManager)
Marc
  • 3,550
  • 22
  • 28
1

This is not an XSLT question, so here is a single XPath expression that selects the two nodes wanted:

/*/*/descendant::a:*[not(count(ancestor::a:*) > 2)]
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Thanks for the input, I went with something very much like this. But because the depth of the starting node was unknown, I did have to calculate it in a separate step. – harpo Jun 04 '10 at 19:52
  • @harpo: In your case, was the starting node known, eg. did you have an XPath expression selecting the starting node? – Dimitre Novatchev Jun 04 '10 at 19:59
  • No, I just had an XmlNode reference. Actually, I ended up building path expressions for the selected nodes using a routine adapted from Jon Skeet's answer to another question, http://stackoverflow.com/questions/241238/how-to-get-xpath-from-an-xmlnode-instance-c/241291#241291 – harpo Jun 05 '10 at 17:46
0

This isn't doable with a single XPath 1.0 expression. Extending on Marc's answer which uses XSLT, you can do this for 1.0:

<xsl:variable name="n" select="count(ancestor-or-self::a:*)" />
<xsl:variable name="result" select=".//a:*[count(ancestor::a:*) = $n]" />

or the equivalent sequence of C# calls for Select....

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289