XML 命名空间冲突 [英] XML namespaces conflicts

查看:36
本文介绍了XML 命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我们必须使用对我们的组织唯一的 URI 来定义我们的命名空间(就像 Java 中的包一样).这显然是有道理的,让我们避免名称冲突.但是,这个页面说:

I was wondering why do we have to define our namespaces with an URI that is unique for our organization (just like packages in Java). That would obviously make sense and let us avoid name conflicts. However, this page says:

也可以(虽然不推荐)使用相同的前缀引用到不同的命名空间,取决于它们的上下文

It is also possible (though not recommended) for the same prefix refer to different namespaces, depending on their context

所以基本上,如果我有自己的 XML 文档,我自己的命名空间前缀 myNamespace 定义为 http://hisdomain.com/test,那么有人可以重新定义myNamespace"' 前缀指向他自己的命名空间http://hisdomain.com/test即使是错误的(我认为如果我定义了一个命名空间前缀,已经在文档中定义了)

So basically if I have my own XML document with my own namespace prefix myNamespace defined as http://hisdomain.com/test, somebody could redefine the 'myNamespace' prefix to point to his own namespace http://hisdomain.com/test, even by mistake (I think there is no alert if I define a namespace prefix that has been already defined in the document).

即使使用 URI,其他人也需要确保他们不使用我的前缀!

Even with URIs, other people need to make sure they don't use my prefix!

然后使用 URI 和必须为命名空间提供定义变得无用,如果我们只使用前缀而不必使用 xlns 属性提供定义,命名空间系统将同样有效.

Then using URIs and having to provide definitions to namespaces becomes useless, the namespace system would work equally well if we simply used the prefixes only without having to provide definitions using xlns attribute.

如果我理解正确,这是不允许的:

If I understand correctly, this is not allowed:

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">
...
</root>

但是这个没问题 - 您可以使用您的域重新定义 abc 前缀.

but this one is okay - you could redefine the abc prefix with your domain.

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>

推荐答案

即使使用 URI,其他人也需要确保他们不使用我的前缀!

Even with URIs, other people need to make sure they don't use my prefix!

正确,在 XML 文档中,命名空间前缀声明不应冲突.

Correct, within an XML document, namespace prefix declarations should not conflict.

然而,关键是命名空间前缀本身并不重要.它是完整命名空间名称的语法糖.符合标准的 XML 处理器不会关心您或其他方使用什么命名空间前缀.命名空间前缀只是方便快捷;命名空间名称很重要.

The key, however, is that the namespace prefix itself is insignificant. It is syntactic sugar for the full namespace name. Conformant XML processors will not care what you or other parties use for namespace prefixes. Namespace prefixes are just shorthand conveniences; namespace names are what matter.

然后使用 URI 并且必须为命名空间提供定义变得毫无用处,如果我们仅使用前缀而无需提供定义使用 xlns 属性.

Then using URIs and having to provide definitions to namespaces becomes useless, the namespace system would work equally well if we simply used the prefixes only without having to provide definitions using xlns attribute.

不是真的.

如果组件名称必须足够长以支持名称空间名称的唯一性要求,那么名称空间系统将无法正常工作.我们不喜欢多余的冗长.

The namespace system would not work equally well if component names were burdened with having to be long enough to support the uniqueness requirements of namespace names. We wouldn't like the extra verbosity.

新问题的更新:

正确,这是不允许的(类似于给定元素的两个属性不能具有相同的名称):

Correct, this is not allowed (similar to how two attributes could not have the same name for a given element):

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">

但这是允许的(但最好避免):

But this is allowed (but better avoided):

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>

这篇关于XML 命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆