XPath: limiting ancestor scope

Viewed 314

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

<root>
  <a id="first" class="x"> 
    <b> 
      <c id="1" class="y"></c> 
    </b>
    <b> 
      <c id="2" class="y"></c>
    </b>
  </a>  
  <a id="second" class="x"> 
    <c id="3" class="y"></c> 
  </a> 
</root>

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

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

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.

0 Answers
Related