问题
When the default namespace and a prefixed namespace resolves to the same namespace URI, why is an attribute with no prefix not equal to a prefixed attribute, when both have the same local name?
The "Namespaces in XML" specification just says it's so, but it's very short on why. Anyone knows why it's like this?
Excerpt from section "6.3 Uniqueness of Attributes" at http://www.w3.org/TR/xml-names11/#uniqAttrs :
For example, each of the bad empty-element tags is illegal in the following:
<!-- http://www.w3.org is bound to n1 and n2 --> <x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" > <bad a="1" a="2" /> <bad n1:a="1" n2:a="2" /> </x>
However, each of the following is legal, the second because the default namespace does not apply to attribute names:
<!-- http://www.w3.org is bound to n1 and is the default --> <x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" > <good a="1" b="2" /> <good a="1" n1:a="2" /> </x>
I think this just makes it harder to parse namespaced XML, since the parser has to check the presence of both the attributes and pick one.
For my case, I like to add Atom links to my XML documents like this:
<root xmlns="..." xmlns:atom="...">
<atom:link rel="self" type=".." href=".." />
</root>
I would think that the attributes on atom:link would inherit the elements namespace. Parsing the XML with DOM in Java reported the Atom namespace for the element, but no namespace for the attributes.
回答1:
Short answer: unprefixed attributes are always in the empty name space, i.e. they have no name space.
In the example:
<good a="1" n1:a="2" />
the first a would expand to
{}a
whereas the second would expand to:
{http://www.w3.org}a
In your atom example, all attributes are in the empty name space.
来源:https://stackoverflow.com/questions/7984397/why-is-an-xml-attribute-without-xmlns-prefix-not-equal-to-a-prefixed-attribute-w