XML : xPath: From parent node, shortcut to sub-sub-sub child node that matches condition?

I'm trying to write a xpath that gets the text from "I want to get this text snippet" below.

  <root>    <div class="box">      <div class="col-lg-12">        <h2>Issuer</h2>      </div>    </div>    <div class="table-responsive">      <table class="table">        <tbody>          <tr>            <td> Name </td>            <td class="text-right"> I want to get this text snippet </td>          </tr>        </tbody>      </table>    </div>  </root>    

So far I have this:

  //h2["Issuer"]/parent::div/parent::div/following-sibling::div//td["Name"]/following-sibling::td/text()    

There are two parts to this line at the moment:

  1. //h2["Issuer"]/parent::div/parent::div/following-sibling::div which takes us to <div class="table-responsive">.
  2. //td["Name"]/following-sibling::td/text() which takes us to the text snippet target in its <td> block.

Both parts are important because there are multiple places in the real webpage source code where Name can be found underneath. So the first part determines where in the overall structure to start looking.

However, I suspect my usage of // is wrong because including it in the second part will override whatever's in the first part. So effectively it's the same as:

  //td["Name"]/following-sibling::td/text()    

Question:

What I'm really trying to accomplish is to find a way to bypass the children nodes below <div class="table-responsive"> and jump directly to whereever Name can be found, and then grab the sibling node which contains the text snippet.

So, instead of what I'm trying to do with // in //td["Name"]/following-sibling::td/text();

What is the proper way to "jump past" nodes and find any matching node underneath?

No comments:

Post a Comment