Search code examples
c#xmlxpathxpath-1.0

Xpath to search descendents of a node


I have the following c# method, that performs some operation on all nodes reachable from refNode, through xpath

void foo(XmlNode refNode, string xpath)
{
    XmlNodeList list=refNode.SelectNodes(xpath);
    //perform operation on each element of the list
}

One of the input xml that i'm getting is:

<A>
    <B>***
        <C>
                  <B>One</B>
            </C>
        <B>
                  <B>Two</B>
            </B>
    </B>
    <B>...</B>
    <B>...</B>
</A>

where i need to select a refNode <B> (marked ***) and pass it to foo() with an xpath that selects all descendent <B> nodes of refNode, but not nested inside any other <B> node

for example in the given input the result should contain:

1. <B>One</B>
2. <B><B>Two</B></B>

I have tried .//B which gives me 3 results and .//B[not(ancesotr::B)] which returns 0 results.

What Xpath should I use to get the desired result?

Edit

I can make changes to method foo, but not its signature. This method is a part of a library and is being used by few users. The input given above is just a specific instance, the user might also send node A as refnode and ask for all C nodes.

Edit2 @Dimitre Novatchev's solution works for me if I can get the xpath of the refnode inside foo without changing its signature or if there is some way to specify this node, i.e. the node on which the xpath is being applied.

.//B[not(ancesotr::B) or ancesotr::B[1]=**this**]

Solution

  • Use this pure XPath 1.0 expression:

    $vrefNode/descendant::B[count(ancestor::B) - count($vrefNode/ancestor::B) = 1]
    

    where $vrefNode needs to be substituted (unless you can use variable references) with the XPath expression that selects the "reference node".

    XSLT - based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "/*/B[1]/descendant::B[count(ancestor::B) - count(/*/B[1]/ancestor::B) = 1]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <A>
        <B>***
            <C>
                <B>One</B>
            </C>
            <B>
                <B>Two</B>
            </B>
        </B>
        <B>...</B>
        <B>...</B>
    </A>
    

    the XPath expression is evaluated and the selected elements by this evaluation are copied to the output:

    <B>One</B>
    <B>
    
       <B>Two</B>
    
    </B>