AnchovyLegend
AnchovyLegend

Reputation: 12538

XML XPath: conditionally selecting table cell

I am working on a project that has the below markup structure. The objective is to select The brown fox (Phrase column), if the table heading contains Developer(s) (as shown below). Is this possible?

<table>
  <tbody>
    <tr>
      <th scope="col"><abbr title="Number">Number</abbr></th>
      <th scope="col">Phrase </th>
      <th scope="col">Developer(s)</th>
      <th scope="col">Pineapple</th>
    </tr>
    <tr>
      <td >1.</td>
      <td>"<a href="#first">The brown fox</a>"</td>
      <td>Bob Smith</td>
      <td>Cool Joe</td>
    </tr>
  </tbody>
</table>

Upvotes: 1

Views: 34

Answers (2)

LMC
LMC

Reputation: 12777

This Xpath will get the expected text

'//table[tr/th[. = "Developer(s)"]]/tr[2]/td[2]/a/text()'

Upvotes: 1

kjhughes
kjhughes

Reputation: 111686

This XPath,

//table[tbody/tr/th="Developer(s)"]
       /tbody/tr/td[position() - 1 =
                    count(../../tr/th[normalize-space()="Phrase"]
                          /preceding-sibling::th)]

selects the td elements in the same position as the "Phrase" th for the table containing a th with a string value equal to "Developer(s)", as requested.

Upvotes: 0

Related Questions