0

Im parsing a table using hpple and libxml2 in an iPhone app. I have encountered a real problem when it comes to finding the column index when sibling cells span multiple rows using colspan.

I saw this question But I can't use jquery to work out the column.

Consider the following table

<table>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td id="example1">Three</td>
      <td>Four</td>
      <td>Five</td>
      <td>Six</td>
    </tr>
    <tr>
      <td colspan="2">One</td>
      <td colspan="2">Two</td>
      <td colspan="2" id="example2">Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
      <td>Four</td>
      <td>Five</td>
      <td>Six</td>
    </tr>
  </tbody>
</table>

How can i get the column index as 6 NOT 3 for the cell with id 'example1'?

EDIT Added more detail

NSString *xpathQuery = [NSString stringWithFormat:@"1 
+ count(//a[contains(@href,'testHref')]
/../preceding-sibling::td[not(@colspan)])
+ sum(//a[contains(@href,'testHref')]/../preceding-sibling::td/@colspan)
+ sum(@colspan)
- count(@colspan)",bookingUrl, bookingUrl];

//Execute XPath
NSArray *array = [parser searchWithXPathQuery:columnCount];
Community
  • 1
  • 1
BON
  • 433
  • 2
  • 9
  • 18

1 Answers1

0

This seems to work:

1 + count(preceding-sibling::td[not(@colspan)])
+ sum(preceding-sibling::td/@colspan)
+ sum(@colspan)
- count(@colspan)

Running the xpath for the <td> as the current node.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • My problem is that Hpple expressions return an array of Nodes. Also, how I identify the cells is a little more complicated... I've added more code to my answer – BON Aug 06 '12 at 19:21