Why is an XML attribute without XMLNS prefix not equal to a prefixed attribute with same local name?

♀尐吖头ヾ 提交于 2021-02-09 11:47:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!