cubesnyc
cubesnyc

Reputation: 1545

XPath conditional predicate

<table>
  <tr>
    <td></td>
    <td rowspan=2></td> 
    <td></td>  <-- select
  </tr>
  <tr>
    <td></td>
    <td></td>  <-- select
  </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>  <-- select
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>  <-- select
  </tr>
</table>

I am trying to construct an expression using xpath 1.0 that will select the 2nd /tr/td node in the event the preceding /tr/td nodeset has a rowspan in it, and 3rd td in all other cases. Is this possible?

EDIT::: to clarify, this is not the actual data where last() would suffice. the actual amount of td nodes is indeterminate.

Upvotes: 0

Views: 181

Answers (2)

SomeDude
SomeDude

Reputation: 14238

You can try this :

//td[count(@rowspan)>0]/following-sibling::*[name()='td'][1] | //td[position()=3 and not ( preceding-sibling::*[name()='td' and count(@rowspan)>0] )]

With the below xml :

<table>
  <tr>
    <td rowspan="2"></td> 
    <td>following rowspan 1</td>
    <td>following rowspan 2</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td>third1</td>
  </tr>
    <tr>
    <td></td>
    <td></td>
    <td>third2</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td>third3</td>
  </tr>
</table>

It will produce :

<td>following rowspan 1</td>
<td>third1</td>
<td>third2</td>
<td>third3</td>

Upvotes: 0

wero
wero

Reputation: 33010

Ugly but works:

/table/tr/td[2][../preceding-sibling::*[1]/td/@rowspan] | /table/tr/td[3][not(../preceding-sibling::*[1]/td/@rowspan)]

Upvotes: 1

Related Questions