Bryant Makes Programs
Bryant Makes Programs

Reputation: 1694

Python LXML XPATH Returns Error With Valid Query

I am running the following query:

//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/string(.)

I get the following error:

lxml.etree.XPathEvalError: Invalid expression

However, if I use the following notation:

//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/text()

All is well.

I assume the issue is with the use of the string(.) notation, but when I test it here it comes up fine, so I'm assuming its valid syntax.

I'm essentially running the following code:

from lxml import html
tree = html.fromstring(PAGE_CONTENT)
results = tree.xpath(QUERY)

Is there an alternative method I could be using that will allow the use of expressions like this? string-join seems to cause similar issues.

Upvotes: 0

Views: 127

Answers (1)

zx485
zx485

Reputation: 29022

The reason for your error is that the notation

.../string(.)

is only valid in XPath 2.0 or above. In XPath 1.0 it is invalid and throws an error.

An alternative that is valid in XPath-1.0 would be wrapping the whole expression in the string(...) function like this:

string(//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p)

Upvotes: 2

Related Questions