tags:

views:

33

answers:

2

What XPATH query could i use to get the values from the 1st and 3rd tag for each row in a html table.

The XPATH query i use is /table/tr/td[1]|td[3]. This only returns the values in the first tag for each row in a table.

EXAMPLE I would expect to get the values bob,19,jane,11,cameron and 32 from the below table. But am only getting bob,jane,cameron.

<table>
<tr><td>Bob</td><td>Male</td><td>19</td></tr>
<tr><td>Jane</td><td>Feale</td><td>11</td></tr>
<tr><td>Cameron</td><td>Male</td><td>32</td></tr>
</table>
+1  A: 

Try

 /table/tr/td[1]|/table/tr/td[3]

I remember doing this in the past and found it rather annoying because it is ugly and long-winded

jakenoble
@jakenoble: right answer, but I don't see the ugliness. Also this is XPath 1.0, but in XPath 2.0 you could use `/table/tr/(td[1]|td[3])`
Alejandro
@Alejandro -- seems incorrect. Probably you meant something like the expression in my answer? :)
Dimitre Novatchev
@Dimitre: jakenoble's answer works for me. Also my XPath 2.0 expression.
Alejandro
+1  A: 

@jakenoble's answer:

/table/tr/td[1]|/table/tr/td[3] 

is correct.

An equivalent XPath expression that avoids the | (union) operator and may be more efficient is:

/table/tr/td[position() = 1 or position() = 3]
Dimitre Novatchev