Search code examples
xmlxpathhtml-parser

help me with xpath please


Possible Duplicate:
if you know xpath then please help?

hi, so here is the html code

<html>
<body>
<table>
<tr>
Test
</tr>
<tr> 
<td>
<a href = "google.com">
Google
</a>
</td>
</tr>

<tr>
<td> test1</td>
<td> 
<a href = "yahoo.com"> Yahoo!</a>
</td>
</tr>


</tr>
</table>

</body>
</html>

so now I want the text Google and Yahoo!

how can I get that

here is what I wrote

table[1]/tr[1]/td[2]

I dont't know what is happening but nothing is showing up...

thanks


Solution

  • The XPath you are looking for is as follows:

    For Google

    /html/body/table/tr[2]/td/a/text()
    

    For Yahoo

    /html/body/table/tr[3]/td/a/text()
    

    As the title is contained in a 'a' element and you use the text() node type test to get the text value of the 'a' element.

    Actually I think your html has a additional </tr> at the bottom which makes it invalid