Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple string() results for an xpath?

Tags:

xpath

string()

works great on a certain webpage I am trying to extract text from.

http://www.bing.com/search?q=lemons&first=111&FORM=PERE

has similar structure. For bing, the xpath I have tried is

string(//h3/a)

which works great to get the search results, even with strong tags etc, but only returns the first result. Is there something like strings(), so I can get the full text of each

//h3/a

result?

like image 767
user964375 Avatar asked Feb 19 '26 22:02

user964375


1 Answers

Is there something like strings(), so I can get the full text of each

//h3/a 

result?

No, Not in XPath 1.0.

From the W3C XPath 1.0 Specification (the only normative document about XPath 1.0):

"Function: string string(object?)

The string function converts an object to a string as follows:

A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order."

So, if you only have an XPath 1.0 engine available, you need to select the node-set of all //h3/a elements and then in your programming language that is hosting XPath, to iterate on each node and get its string value separately.

In XPath 2.0 use:

//h3/a/string()

The result of evaluating this XPath 2.0 expression is a sequence of strings, each of which is the string value of one of the//h3/a elements.

like image 194
Dimitre Novatchev Avatar answered Feb 21 '26 14:02

Dimitre Novatchev