简体   繁体   中英

Select Xpath Element Based on Child Node Value

Let's say I have a section of an xpath that looks like

<parent elements>
    <div>
        <h2>Dog</h2>
    <div>
        <h2>Cat</h2>
        <table>
            <tbody>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Target</tr>
            </tbody>
    <div>

I first need to narrow it down to only the div where the value inside the h2 tag is == "Cat". After this, I need to drill down into said div and extract the 5th tr element.

The problem I'm trying to overcome is that all the divs have unique h2's, but they are in random order so sometime the div with an h2 == "Cat" might be ./div[1] whereas other times it might be ./div[5]. Once I've identified the div with the proper h2 (unique identifier) I can then reliably always extract exactly the 5th tr element.

I'm currently trying the code below but it only returns True/False

./parent_elements[div/h2 = "Web Site Information"]

Many thanks!

As your example input XML is not valid, I've just adjusted it to this:

<parent_elements>
  <div>
    <h2>Dog</h2>
    <div>
       <h2>Cat</h2>
       <table>
          <tbody>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Target</tr>
          </tbody>
       </table>
    </div>
  </div>
</parent_elements>

For this example, the following XPath

//parent_elements//div[h2='Cat']//tr[5]

has the result

<tr>Target</tr>

This XPath selects the fifth tr that is a child of an h2 element with the value Cat which is a child element of parent_elements .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM