Note that there are some explanatory texts on larger screens.

plurals
  1. POXPath: limit scope of result set
    text
    copied!<p>Given the XML</p> <pre><code>&lt;a&gt; &lt;c&gt; &lt;b id="1" value="noob"/&gt; &lt;/c&gt; &lt;b id="2" value="tube"/&gt; &lt;a&gt; &lt;c&gt; &lt;b id="3" value="foo"/&gt; &lt;/c&gt; &lt;b id="4" value="goo"/&gt; &lt;b id="5" value="noob"/&gt; &lt;a&gt; &lt;b id="6" value="near"/&gt; &lt;b id="7" value="bar"/&gt; &lt;/a&gt; &lt;/a&gt; &lt;/a&gt; </code></pre> <p>and the Xpath 1.0 query</p> <pre><code>//b[@id=2]/ancestor::a[1]//b[@value="noob"] </code></pre> <p>The Xpath above returns both node ids 1 and 5. The goal is to limit the result to just node id=1 since it is the only <code>@value="noob"</code> element that is a descendant of the same <code>&lt;a&gt;</code> that (<code>//b[@id=2]</code>) is also a descendant of.</p> <p>In other words, "Find all <strong>b</strong> elements who's value is "noob" that are descendants of the <strong>a</strong> element which also has a descendant whose id is 2, but is not the descendant of any other <strong>a</strong> element". How's that for convoluted? In practice the id number and values would be variable and there would hundreds of node types.</p> <p>If the id=2, we would expect to return element id=1 not id=5 since it is contained in another a element. If the id=4, we would expect to return id=5, but not id=1 since it is not in the first ancestor a element as id=4.</p> <p><strong>Edit:</strong> Based on the comments of Dimitre and Alejandro, I found <a href="http://sweatte.wordpress.com/syntax/xml-tips/xpath-tips/" rel="nofollow">this</a> helpful blog entry explaining the use of count() with the | union operator as well as some other excellent tips. </p>
 

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