Partially support parenthesized xpath#317
Open
tompng wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates REXML’s XPath parsing/evaluation to better handle predicates applied to parenthesized node-set expressions (e.g., (/path[p1])[p2]), addressing the node-set position semantics behind issue #25 and adjusting behavior introduced by #122.
Changes:
- Fix predicate evaluation so positions are computed per nodeset (not across multiple nodesets), correcting cases like
/div/div/test[1][2]. - Add a parsing workaround for parenthesized node-set expressions by inserting a
self::node()“separator” step to distinguish inner vs. outer predicates. - Expand XPath tests to cover nested predicate/parentheses scenarios and axis expressions wrapped in parentheses.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/xpath/test_base.rb | Adds regression tests for nested predicates and parenthesized expressions. |
| lib/rexml/xpath_parser.rb | Adjusts predicate evaluation to assign positions per nodeset during filtering. |
| lib/rexml/parsers/xpathparser.rb | Inserts a self::node() separator after parenthesized node-set expressions to separate predicate scopes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| matches = XPath.match(doc, '//div[1]/test|//div[2]/test[2]').map(&:text) | ||
| assert_equal ["ab", "cd", "gh"], matches | ||
| matches = XPath.match(doc, '(//div[1]/test|//div[2]/test)[2]').map(&:text) | ||
| assert_equal ["ab", "cd", "ef", "gh"], matches |
| parsed.concat(n) | ||
| # For xpath like `(/path[predicate1][predicate2])[predicate3][predicate4]`, | ||
| # add a separator mark to distinguish predicates of the inner parentheses and the outer parentheses. | ||
| parsed << :self << :node if NODE_OPERATOR_TYPES.include?(n.first) |
Comment on lines
+16
to
+21
| # Types that evaluates to nodeset | ||
| NODE_OPERATOR_TYPES = %i[ | ||
| document self child literal attribute namespace parent ancestor | ||
| ancestor_or_self descendant_or_self descendant following_sibling | ||
| preceding_sibling preceding following union | ||
| ] |
1760158 to
22ed263
Compare
Add a new path_stack operator `:group`
22ed263 to
10bde7a
Compare
Comment on lines
+442
to
+450
| when :group | ||
| sub_expression = path_stack.shift | ||
| result = expr(sub_expression, nodeset, context) | ||
| if result.is_a?(Array) | ||
| # If result is a nodeset, apply following predicates | ||
| path_stack.unshift(:node) | ||
| nodeset = step(path_stack) do | ||
| [result] | ||
| end |
| sub_expression = path_stack.shift | ||
| result = expr(sub_expression, nodeset, context) | ||
| if result.is_a?(Array) | ||
| # If result is a nodeset, apply following predicates |
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.
Fix #25, and also reverts #122.
Adds a new path_stack operator
:group