-2

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

Community
  • 1
  • 1
Tushar Chutani
  • 1,522
  • 5
  • 27
  • 57

2 Answers2

0

If you know there will always be that number and type of elements:

/html/body/table/tr/td/a/

A more generic way is:

html//a
Mike
  • 4,722
  • 1
  • 27
  • 40
  • Assuming your HTML doc is valid XML, which in this case it is. In general make sure you're using XHTML. – Mike Mar 13 '11 at 05:16
0

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

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • `text()` is not a function but but a node type test. And it's not a good idea to select text nodes in a mixed content model like XHTML... –  Mar 13 '11 at 20:53
  • understood, I have edited the answer regarding describing text() as function, however do you have an alternative to text(), it does answer the question regarding the content that was asked – rogermushroom Mar 13 '11 at 21:37
  • If the result must be a node set, then the proper way is to select those `a` elements and then to get their string value by the proper DOM method. If the result can be just one node' string value, then you could use the `string()` XPath's function. –  Mar 13 '11 at 21:45