XML 命名空间和属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:58:31  来源:igfitidea点击:

XML namespaces and attributes

xmlxsdnamespaces

提问by user4315

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...

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

回答by Bart Schuller

Most of the time, attributes will not be in any namespace. The namespace specsays (emphasismine):

大多数时候,属性不会在任何命名空间中。该命名空间规范说(重点煤矿):

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

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

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.

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

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.

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

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, evenwhen you use a default namespace.

最后,W3C XML Schema 有一种非常简单的方法 ( <schema attributeFormDefault="qualified">) 将属性声明为位于命名空间中,强制您在文档中为它们添加前缀,即使您使用默认命名空间也是如此。

回答by jelovirt

Examples to illustrate using the Clark notation, where the namespace prefix is replaced with the namespace URL in curly brackets:

使用Clark 符号说明的示例,其中命名空间前缀替换为大括号中的命名空间 URL:

<bar xmlns:foo="http://www.foo.com/"
    foo:baz="baz"
    qux="qux"/>
<bar xmlns="http://www.foo.com/" xmlns:foo="http://www.foo.com/"
    foo:baz="baz"
    qux="qux"/>
<foo:bar xmlns="http://www.foo.com/" xmlns:foo="http://www.foo.com/"
    foo:baz="baz"
    qux="qux"/>

is

<{}bar
    {http://www.foo.com/}baz="baz"
    {}qux="qux"/>
<{http://www.foo.com/}bar
    {http://www.foo.com/}baz="baz"
    {}qux="qux"/>
<{http://www.foo.com/}bar
    {http://www.foo.com/}baz="baz"
    {}qux="qux"/>

回答by Diego Tercero

There's something related to this attributes/namespaces subject that took me some time to understand today when I was working on a XSD. I'm going to share this experience with you in case anyone ever happens to have the same issues.

今天我在处理 XSD 时,有一些与这个属性/命名空间主题相关的东西花了我一些时间来理解。如果有人碰巧遇到同样的问题,我将与您分享这一经验。

In the Schema Document I was working on there were a couple of global attributes referenced by some elements. To simplify things here let's assume this XSD I'm talking about was about a Customer.

在我正在处理的架构文档中,一些元素引用了几个全局属性。为了简化这里的事情,让我们假设我正在谈论的这个 XSD 是关于Customer 的

Let's call one of these global attributes Id. And the root element using it Customer

让我们将这些全局属性之一称为Id。以及使用它的根元素Customer

My XSD declaration looked like this :

我的 XSD 声明如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema  xmlns="http://schemas.mycompany.com/Customer/V1" 
targetNamespace="http://schemas.mycompany.com/Customer/V1" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">

My Idattribute declaration looked like this :

我的Id属性声明如下所示:

<xs:attribute name="Id" type="xs:positiveInteger"/>

And my Customerelement used the attribute like this :

我的Customer元素使用了这样的属性:

<xs:element name="Customer">
   <xs:complexType>
      <xs:attribute ref="Id" use="required"/>
      <!-- some elements here -->
    </xs:complexType>
</xs:element>

Now, let's say I wanted to declare a CustomerXML document like this :

现在,假设我想像这样声明一个CustomerXML 文档:

<?xml version="1.0" encoding="utf-8"?>
<Customer Id="1" xmlns="http://schemas.mycompany.com/Customer/V1">
  <!-- ... other elements here -->
</Customer>

I found out that I can't : when the attribute is globally declared, it's not in the same namespace than the element who references it.

我发现我不能:当属性被全局声明时,它与引用它的元素不在同一个命名空间中。

I figured out that the only solution with the XSD defined like that was to declare the namespace twice: once without a prefix in order to make it the default namespace for elements, and once with a prefix in order to use it with the attributes. So this is how it would have looked like :

我发现使用 XSD 定义的唯一解决方案是两次声明命名空间:一次没有前缀以使其成为元素的默认命名空间,一次带有前缀以便将其与属性一起使用。所以这就是它的样子:

<?xml version="1.0" encoding="utf-8"?>
<Customer cus:Id="1" xmlns="http://schemas.mycompany.com/Customer/V1"
 xmlns:cus="http://schemas.mycompany.com/Customer/V1">
  <!-- ... other elements here -->
</Customer>

This is so unpractical that I just decided to get rid of all global attributes and declare them them locally. Wich in the case of the example I gave here would have looked like this:

这太不切实际了,以至于我决定摆脱所有全局属性并在本地声明它们。在我在这里给出的例子中,它看起来像这样:

<xs:element name="Customer">
   <xs:complexType>
       <xs:attribute name="Id" type="xs:positiveInteger" use="required"/>
       <!-- some elements here -->
   </xs:complexType>
</xs:element>

I found it hard to find some references about what I'm talking about here in the net. I eventually found this postin the Stylus XSD Forum where a guy named Steen Lehmann suggested either to declare the attribute locally or to declare it within an attribute group

我发现很难在网上找到一些关于我在这里谈论的内容的参考资料。我最终在 Stylus XSD 论坛中找到了这篇文章,其中一个名叫 Steen Lehmann 的人建议要么在本地声明属性,要么在属性组中声明它

"so that the attribute declaration itself is no longer global"

“这样属性声明本身就不再是全局的”

This last solution has a "hacky" taste, so I just decided to stick with the first solution and declare all my attributes locally.

最后一个解决方案有一种“hacky”的味道,所以我决定坚持第一个解决方案并在本地声明我的所有属性。

回答by Staale

Read up at 6.1 Namespace Scopingand 6.2 Namespace Defaultingon w3c.

阅读w3c上的6.1 Namespace Scopeing6.2 Namespace Defaulting

Basically:

基本上:

The scope of a namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag

声明前缀的命名空间声明的范围从它出现的开始标记的开头延伸到相应结束标记的结尾

However, the text here doesn't seem to explain if means a is foo:a or the default namespace in the context. I would assume that it does not refer to foo:a, but rather the documents default namespace a. Considering this quote at least:

然而,这里的文字似乎没有解释是否意味着 a 是 foo:a 或上下文中的默认命名空间。我认为它不是指 foo:a,而是指文档默认命名空间 a。至少考虑一下这个报价:

Such a namespace declaration applies to all element and attribute names within its scope whose prefix matches that specified in the declaration.

这种命名空间声明适用于其范围内的所有元素和属性名称,其前缀与声明中指定的前缀相匹配。

Ie. the namespace "foo:" only applies to elements prefixed with foo:

IE。命名空间“foo:”仅适用于以 foo 为前缀的元素: