XML 命名空间和属性 [英] XML namespaces and attributes

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

问题描述

我试图了解命名空间在 XML 中的工作原理.当我有一个像 foo:bar 这样的元素时,属性上通常没有命名空间.但有时他们会.属性是否在元素的命名空间中,即使已经声明了默认命名空间?查看 xhtml 的 xsd 似乎属性是架构的一部分,应该在 xhtml 的命名空间中,但它们从未以这种方式呈现...

I'm trying to understand how namespaces work in XML. When I have an element like foo:bar, the attributes will often not have namespaces on them. But sometimes they will. Are the attribute in the namespace of the element, even when the default namespace has been declared? Looking over the xsd for xhtml it seems the attributes are part of the schema and should be in the namespace for xhtml, but they are never presented that way...

推荐答案

大多数时候,属性不会在任何命名空间中.命名空间规范说(强调我的):

Most of the time, attributes will not be in any namespace. The namespace spec says (emphasis mine):

默认命名空间声明适用于其范围内所有不带前缀的元素名称.默认命名空间声明直接应用于属性名称;无前缀属性的解释由它们出现的元素决定.

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

大多数 XML 词汇使用非命名空间属性是有原因的:
当您的元素具有命名空间并且这些元素具有属性时,就不会混淆:属性属于您的元素,而元素属于您的命名空间.为属性添加命名空间前缀只会让一切变得更加冗长.

There's a reason that most XML vocabularies use non-namespaced attributes:
When your elements have a namespace and those elements have attributes, then there can be no confusion: the attributes belong to your element, which belongs to your namespace. Adding a namespace prefix to the attributes would just make everything more verbose.

那么为什么命名空间属性存在?
因为一些词汇表可以用大多数属性来做有用的工作,并且在与其他词汇表混合时可以做到这一点.最著名的例子是 XLink.

So why do namespaced attributes exist?
Because some vocabularies do useful work with mostly attributes, and can do this when mixed in with other vocabularies. The best known example is XLink.

最后,W3C XML Schema 有一种非常简单的方法 (<schema attributeFormDefault="qualified">) 将您的属性声明为在命名空间中,强制您在文档,甚至当您使用默认命名空间时.

Lastly, W3C XML Schema has an all too easy way (<schema attributeFormDefault="qualified">) of declaring your attributes as being in a namespace, forcing you to prefix them in your documents, even when you use a default namespace.

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

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