0

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.

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Dang, thanks friend. Wrapping it in a string unfortunately concatenates all results. And BLAH/string(p) likewise throws an error. Will have to revisit the drawing board me thinks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.