Document how Element#attribute matches a namespace - #360
Merged
Conversation
REXML::Element#attribute matches its +namespace+ argument loosely: an
unprefixed attribute is taken to be in the default namespace, and a
namespace that no prefix is bound to falls back to the unprefixed
attribute.
<root xmlns='ns0' a='a'/>
attribute("a", "ns0") -> a='a'
attribute("a", "nosuch") -> a='a'
The XML Namespaces specification says an unprefixed attribute has no
namespace and does not take the default one, so neither of those should
match. The
behavior is kept for compatibility -- it is what tickets 102 and 121
asked for, and the tests for both are still in place -- but nothing said
so, which has left people reading the method unsure whether what they
saw was the contract or a bug.
Say it in the documentation of both methods, and point each at the
other: REXML::Attributes#get_attribute_ns matches as the XML Namespaces
specification says and is what to reach for when the namespace has to be
matched strictly. It has behaved that way at least as far back as rubyGH-151, but
that discussion never mentioned it.
GitHub: rubyGH-151
Contributor
Author
|
Based on the discussion below, we will update the documentation for |
kou
pushed a commit
that referenced
this pull request
Aug 31, 2026
GH-360 documented `REXML::Element#attribute` as matching a namespace loosely, and said an unprefixed attribute is taken to be in the default namespace. GH-357 made the method ask REXML::Attributes#get_attribute_ns first, so that is now only true when nothing matches strictly: <root xmlns='ns0' xmlns:p='ns0' a='A' p:a='PA'/> attribute("a", "ns0") -> p:a='PA', not a='A' The two examples in the note still hold, because neither document has a prefixed attribute of the same local name for the strict match to find. It is the rule stated around them that no longer holds: an unprefixed attribute is taken to be in the default namespace only when nothing matches strictly. Describe the method the way it now works -- strict first, then the compatibility fallback -- and add the example above, which is the only place the change is visible. Say of get_attribute_ns that it matches strictly and only strictly, rather than pointing at it as the way to match strictly, now that Element#attribute starts there too.
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.
GitHub: Fix GH-151
REXML::Element#attributematches its +namespace+ argument loosely: an unprefixed attribute is taken to be in the default namespace, and a namespace that no prefix is bound to falls back to the unprefixed attribute.The XML Namespaces specification says an unprefixed attribute has no namespace and does not take the default one, so neither of those should match. The
behavior is kept for compatibility -- it is what tickets 102 and 121 asked for, and the tests for both are still in place -- but nothing said so, which has left people reading the method unsure whether what they saw was the contract or a bug.
Say it in the documentation of both methods, and point each at the other: REXML::Attributes#get_attribute_ns matches as the XML Namespaces specification says and is what to reach for when the namespace has to be matched strictly. It has behaved that way at least as far back as GH-151, but that discussion never mentioned it.
Reported by Hiroya Fujinami. Thanks!!!