XPath: limiting ancestor scope
<p>I am trying to create an xpath statement that finds the next element within a containing context that matches a particular class but not in other contexts. For example, given the following xml</p>

<pre><code>&lt;root&gt;
  &lt;a id="first" class="x"&gt; 
    &lt;b&gt; 
      &lt;c id="1" class="y"&gt;&lt;/c&gt; 
    &lt;/b&gt;
    &lt;b&gt; 
      &lt;c id="2" class="y"&gt;&lt;/c&gt;
    &lt;/b&gt;
  &lt;/a&gt;  
  &lt;a id="second" class="x"&gt; 
    &lt;c id="3" class="y"&gt;&lt;/c&gt; 
  &lt;/a&gt; 
&lt;/root&gt;
</code></pre>

<p>starting from id 1, I want to find the next y class element within the "first" a instance, but not anything from the "second" a instance. I have </p>

<pre><code>//c[@id="1"]/ancestor::*/following-sibling::*[1]//descendant-or-self::*[@class="y"]
</code></pre>

<p>but this returns elements of id 2 and 3, when I only want id 2. I've seen the post titled "XPath: limit scope of result set" on stackoverflow, but it seems to be addressing a different problem or I don't understand that solution.</p>