xml - 为什么没有 XMLNS 前缀的 XML 属性不等于具有相同本地名称的前缀属性?

当默认命名空间和带前缀的命名空间解析为相同的命名空间 URI 时,为什么没有前缀的属性不等于带前缀的属性,而两者具有相同的本地名称?

“XML 中的命名空间”规范只是说是这样,但并没有说明原因。 有人知道为什么会这样吗?

摘自“6.3 属性的唯一性”部分 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>

我认为这只会让解析命名空间的 XML 变得更加困难,因为解析器必须检查两个属性的存在并选择一个。

就我而言,我喜欢像这样向我的 XML 文档添加 Atom 链接:

<root xmlns="..." xmlns:atom="...">
    <atom:link rel="self" type=".." href=".." />
</root>

我认为 atom:link 上的属性会继承元素命名空间。在 Java 中使用 DOM 解析 XML 报告了元素的 Atom 命名空间,但没有属性的命名空间。

最佳答案

简答:没有前缀的属性总是在空的命名空间中,即它们没有命名空间。

在例子中:

<good a="1"     n1:a="2" />

第一个 a 将扩展为

{}a

而第二个将扩展为:

{http://www.w3.org}a

在您的原子示例中,所有属性都在空 namespace 中。

https://stackoverflow.com/questions/7984397/

相关文章:

ruby-on-rails-3 - Solr/太阳黑子/Rails 3 错误 : undefined

drupal - 在 drupal 7 中将表单元素添加到节点以供用户输入

wpf - 从 DataTemplate 中的控件获取 GridViewColumn

Ruby Gem 安装失败 - 执行 gem 时出错 - 未初始化的常量 Syck::Syck

.net - 有没有人使用 DbEnumerator

sql-server - 'Insert into linked table failed, RAI

html-agility-pack - html 敏捷包和文本中的三角括号

wpf - 如何使用不在代码隐藏中的 XAML 绑定(bind) ListView ItemsSou

hibernate - 在 Java 构建路径中添加 jar 之前,无法通过在 pom.xml 中添

validation - 如何验证电子商务网站的电子邮件地址?