-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Xpath doesn't support ancestor-or-self on child elements #1652
Comments
The Xpath is evaluated on the specific element - you can see an example in this test: jsoup/src/test/java/org/jsoup/select/XpathTest.java Lines 40 to 54 in 7f28cb0
That works as the w3c document is constructed from the scoped input element. That was implemented as an optimization vs constructing the w3c doc around the entire jsoup doc as I had anticipated that queries would generally select "down" (the current or lower elements) and not up - so by constructing just on the portion of the tree in use, it would be more efficient. So, the issue you are seeing with the I think it would make sense to change the implementation to construct around the entire document, and then directly scope the query, if the xpath evaluator will support that. That would enable this use case and other direct axes like |
Thanks, fixed! |
Hello,
I was trying a really simple XPath expression to retrieve all the ancestors of a specified element but it returned an empty Elements object :
Elements ancestors = element.selectXpath("ancestor-or-self::*");
Debugging it, I saw that in the org.jsoup.nodes.NodeUtils.selectXpath(String, Element, Class) method, you did that :
The XPath expression is always executed at document level, which pretty much defies the purpose... (or at least isn't at all intuitive when applying it to a specific element)
If there is a way to apply relative XPath expressions, it would be really welcome.
The text was updated successfully, but these errors were encountered: