Skip to content

Fix XPath matching for the predefined XML namespace and empty strings - #398

Merged
naitoh merged 2 commits into
ruby:masterfrom
akzarma:fix/xpath-xml-namespace-and-predicates
Dec 2, 2026
Merged

naitoh merged 2 commits into
ruby:masterfrom
akzarma:fix/xpath-xml-namespace-and-predicates

Conversation

@akzarma

@akzarma akzarma commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #396.

The XML parser already knows the reserved xml binding, but XPath was looking it up in each element’s declared namespaces. For <r lang="other" xml:lang="en"/>, //@xml:* with an explicit XML namespace map returned no attributes. The change shares the reserved binding between the parser, the node namespace lookup, and XPath. It also covers lookup by a different expression prefix and namespace-uri(), without adding a synthetic declaration to the document.

The other failure in the issue was independent: Ruby treats an empty string as true. Non-numeric predicates now use XPath’s existing boolean conversion, while numeric predicates still compare against the context position. A regression test keeps a text node containing 0 but drops a whitespace-only one after normalize-space(.).

Both new tests failed on master (f073640a). On macOS, Ruby 4.0.1: bundle exec rake test and the frozen-string-literal run each passed (877 tests, 2,798 assertions); bundle exec rake warning:error rdoc also passed. Other platforms and Ruby versions were not tested locally.

AI assistance: used OpenAI Codex to develop the patch and regression tests and run the checks above.

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
The fixes for both the predefined xml namespace and empty-string predicates look very clean.

One small request: I'd like to avoid introducing a new public constant REXML::Namespace::XML_URI (which is also exposed on REXML::Element and REXML::Attribute since they include REXML::Namespace).

Could you make XML_URI private with private_constant :XML_URI in lib/rexml/namespace.rb and keep Private::XML_PREFIXED_NAMESPACE in lib/rexml/parsers/baseparser.rb as-is?

@akzarma

akzarma commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @naitoh. XML_URI is now private, and Private::XML_PREFIXED_NAMESPACE in baseparser.rb is unchanged from upstream. Both XPath regressions pass, and the full suite on current master with the patch passed (880 tests, 2,812 assertions). Could you take another look when you have a chance?

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@naitoh
naitoh merged commit 41cd279 into ruby:master Dec 2, 2026
71 checks passed
@naitoh

naitoh commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XPath misses xml:* attributes and treats empty strings as true in predicates

2 participants