tags:

views:

130

answers:

3

I have such content of html file:

<a class="bf" title="Link to book" href="/book/229920/">book name</a>

Help me to construct xpath expression to get link text (book name). I try to use /a, but expression evaluates without results.

+2  A: 

If the context is the entire document you should probably use // instead of /. Also you may (not sure about that) need to get down one more level to retrieve the text. I think it should look like this

//a/text()

EDIT: As Tomalak pointed out it's text() not text

pablochan
There is no `<text>` element under `<a>`. You mean `//a/text()`.
Tomalak
A: 

Have you tried

//a

?

More specific is better:

//a[@class='bf' and starts-with(@href, '/book/')]
Tomalak
your answer helped me more, but without starts-with parameter!
alex
@alex: It was just meant to be an example of "more specific is better". ;)
Tomalak
A: 

It depends also on the rest of your document. If you use // in the beginning all the matching nodes will be returned, which might be too many results in case you have other links in your document.

Apart from that a possible xpath expression is //a/text().

The /a you tried only returns the a-tag itself, if it is the root element. To get the link text you need to append the /text() part.

Frank