Repository navigation
[JXPATH-207] DOMNodePointer.asPath() can point to the wrong sibling for a namespaced element - #294
Open
sharanggupta wants to merge 1 commit into
Open
sharanggupta wants to merge 1 commit into
sharanggupta wants to merge 1 commit into
Conversation
…or a namespaced element For a namespaced element without a usable prefix, asPath() falls back to node()[N]. The node() test matches every kind of child (text, comment, processing instruction, CDATA and element), but N was computed by getRelativePositionOfElement(), which only counted element siblings. So for <root>before<item xmlns="urn:test"/></root> asPath() returned /root[1]/node()[1], which resolves to the text node, not the element. The position is now counted over all preceding siblings, which is also what the method's Javadoc already said. The method is renamed to getRelativePositionAmongSiblings() since it no longer counts elements only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes JXPATH-207.
What
DOMNodePointer.asPath()can return a path that resolves to a different node than the pointer it was called on. For a namespaced element with no usable prefix (for example a default-namespace element) it falls back tonode()[N]. Thenode()test (DOMNodePointer.testNode,NODE_TYPE_NODE) matches every kind of child, butNcame fromgetRelativePositionOfElement(), which counted only element siblings. With<root>before<item xmlns="urn:test"/></root>,asPath()returned/root[1]/node()[1], which selects the text nodebefore.How
The position is now counted over all preceding siblings, which is what
node()[N]selects and what the method's Javadoc already said ("among all siblings"). The private method is renamedgetRelativePositionAmongSiblings()because it no longer counts elements only. It is only used by this fallback inasPath(), so no other path changes.Tests (
JXPath207Test)asPath()is/root[1]/node()[2]and resolves back to the same element;node()[4],node()[6]) resolve back to their own element;/root[1]/node()[1]).Without the fix, 2 of the 3 tests fail (
/root[1]/node()[1]resolves to the text nodea); with it all 418 tests pass. Verified locally with the defaultmvngoal on Java 17 (RAT, japicmp, Checkstyle 0 violations, PMD, Javadoc) and the test suite on Java 8. The single skipped test is the existing one inJDOMModelTest.Before you push a pull request, review this list:
mvn; that'smvnon the command line by itself.