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

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

问题描述

我试图了解名称空间在XML中的工作方式.当我有一个像foo:bar这样的元素时,这些属性通常将没有名称空间.但是有时候他们会的.即使声明了默认名称空间,属性也位于元素的名称空间中吗?查看xsd中的xhtml,似乎属性是架构的一部分,应该在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):

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

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">),将属性声明为位于名称空间中,从而迫使您在文档中使用 even 作为前缀.默认名称空间.

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天全站免登陆