XML : XPath: limiting ancestor scope

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

  <a id='first' class='x'>    <b>      <c id='1' class='y'>      <c\>    <b\>    <c id='2' class='y'>    <c\>  <a\>  <a id='second' class='x'>    <c id='3' class='y'>    <c\>  <a\>    

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

  //[@id="1"]/ancestor::div[@class="y"][1]    

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.

No comments:

Post a Comment