Note that there are some explanatory texts on larger screens.

plurals
  1. POXpath to search descendents of a node
    primarykey
    data
    text
    <p>I have the following c# method, that performs some operation on all nodes reachable from refNode, through xpath</p> <pre><code>void foo(XmlNode refNode, string xpath) { XmlNodeList list=refNode.SelectNodes(xpath); //perform operation on each element of the list } </code></pre> <p>One of the input xml that i'm getting is:</p> <pre><code>&lt;A&gt; &lt;B&gt;*** &lt;C&gt; &lt;B&gt;One&lt;/B&gt; &lt;/C&gt; &lt;B&gt; &lt;B&gt;Two&lt;/B&gt; &lt;/B&gt; &lt;/B&gt; &lt;B&gt;...&lt;/B&gt; &lt;B&gt;...&lt;/B&gt; &lt;/A&gt; </code></pre> <p>where i need to select a refNode <code>&lt;B&gt;</code> (marked <code>***</code>) and pass it to foo() with an xpath that selects all descendent <code>&lt;B&gt;</code> nodes of refNode, but not nested inside any other <code>&lt;B&gt;</code> node</p> <p>for example in the given input the result should contain:</p> <pre><code>1. &lt;B&gt;One&lt;/B&gt; 2. &lt;B&gt;&lt;B&gt;Two&lt;/B&gt;&lt;/B&gt; </code></pre> <p>I have tried .//B which gives me 3 results and .//B[not(ancesotr::B)] which returns 0 results.</p> <p>What Xpath should I use to get the desired result?</p> <p><strong>Edit</strong></p> <p>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.</p> <p><strong>Edit2</strong> @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 <code>this</code> node, i.e. the node on which the xpath is being applied.</p> <pre><code>.//B[not(ancesotr::B) or ancesotr::B[1]=**this**] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload