Feffer
Feffer

Reputation: 173

XPath: Using substring-after returns only one match

My problem using XPath is whenever i use the "substring" function I get only one match and I want to get them all. another problem is whenever I use the combination of "substring" and operator | it just won't work (no matches).

For example: http://www.tripadvisor.com/Hotel_Review-g52024-d653910-Reviews-Ace_Hotel_Portland-Portland_Oregon.html

on this webpage I used the query

//SPAN[@class='ratingDate relativeDate']/@title | //*[@class='ratingDate']/text()

I got 10 matches but some of them start with "Reviewed ". so I added "substring-after" and didn't get any matches

the original syntax:

//SPAN[@class='ratingDate relativeDate']/@title  | substring-after(//*[@class='ratingDate']/text(), 'Reviewed ')

Upvotes: 1

Views: 426

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

With pure XPath 1.0 you can't solve that, if you use XPath 2.0 or XQuery 1.0 you can put the substring-after call into the last step of the path e.g. //*[@class='ratingDate']/substring-after(., 'REVIEWED').

If you only have XPath 1.0 then you first need to select the elements with XPath and then iterate over the result in your host language to extract the substring for each element; how you do that depends on the host language and the XPath API.

Upvotes: 3

Related Questions