tags:

views:

34

answers:

1

I have some xml that looks like this...

   <tbody>
     <tr>
       <td>
         <h5>
           <a class="foo" name="bar">This is the element I want to condition on.</a>
         </h5>
       </td>
    </tr>
    <tr>
      <td>
        <a href="/generic/generic">This is the element I want to match on</a>
      <td>
      <td></td>
      <td></td>
   </tr>
  <tbody>

How do I match on the second only the first 's name attribute is "bar", using xpath?

+3  A: 

This expression will work...

//tr[preceding-sibling::tr/td/h5/a/@name = 'bar']/td/a

But there are also some errors in the XML. Here's an edited version:

<tbody>
     <tr>
       <td>
         <h5>
           <a class="foo" name="bar">This is the element I want to condition on.</a>
         </h5>
       </td>
    </tr>
    <tr>
      <td>
        <a href="/generic/generic">This is the element I want to match on</a>
      </td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
  </tbody>
Drew Wills
works nicely thanks!
Rasputin Jones
+1 Good answer.
Alejandro