softwarevamp
softwarevamp

Reputation: 857

How to select nodes with longest text with xpath?

I am trying to query for anchor with longest text by below:

//a[not(string-length(.) < ancestor::li//a/string-length(.))]

Seems it works in xpath 2.0 but not works in python lxml.

I am not very sure about this. Can anyone help explain in more detail? Thanks!

Upvotes: 2

Views: 462

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

Your XPath 2.0 expression actually finds all the a elements that are longer than any other a element within the same li; it will not select any a elements that are not within an li.

A better XPath 2.0 solution might be

for $m in max(//a/string-length()) return //a[string-length(.)=$m]

I don't think it can be done in XPath 1.0.

Upvotes: 1

Related Questions