Silviu
Silviu

Reputation: 93

XPath text() expression that contains a new line

Let's say I have the following HTML code:

<a href="/site/somesite/">
                          somesite</a>

My question is how can I write an XPath expression that must use the text() property to match the somesite link and I cannot change the source?

Upvotes: 9

Views: 8120

Answers (2)

John Kugelman
John Kugelman

Reputation: 362037

I'm not sure whether you want to lookup the URL based on the link text, or the link text based on the URL. This will get you the URL:

//a[normalize-space() = 'somesite']/@href

This will get you the text:

normalize-space(//a[@href = '/site/somesite/'])

Upvotes: 7

Mads Hansen
Mads Hansen

Reputation: 66781

Use normalize-space(), which will throw away the leading and trailing whitespace characters(and condense repeating spaces in the middle of the text into a single space), so that you can compare the normalized text() and use to filter in a predicate.

a[normalize-space(text())='somesite']

Upvotes: 2

Related Questions