You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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 ancestor-or-self axis not working is due to that optimization - the parent structure is not present, so there are no ancestors.
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 preceding-sibling.
jhy
changed the title
Xpath doesn't evaluate on the selected node
Xpath doesn't support ancestor-or-self on child elements
Oct 16, 2021
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: