XML Schema Header &命名空间配置 [英] XML Schema Header & Namespace Config

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

问题描述

从 DTD 迁移到 XSD,出于某种原因,这个转换是一个坎坷的过渡.一旦我进入 <xs:schema> 根标记,我就知道如何定义架构,但是越过了标题 &事实证明,命名空间声明的内容让我特别困惑.

Migrating from DTD to XSD and for some reason the transition is a bumpy one. I understand how to define the schema once I'm inside the <xs:schema> root tag, but getting past the header & namespace declaration stuff is proving to be especially confusing for me.

我一直在尝试遵循 W3S 上精心布置的教程,但即使是该教程似乎也假设前面有很多知识.

I have been trying to follow the well-laid out tutorial on W3S but even that tutorial seems to assume a lot of knowledge up front.

我想我正在寻找的是 King 对哪些属性的作用、它们的去向以及原因的英文解释:

I guess what I'm looking for is a King's English explanation of which attributes do what, where they go, and why:

  • xmlns
  • xmlns:xs
  • xmlns:xsi
  • 目标命名空间
  • xsi:schemaLocation

在某些情况下,我看到这些元素/属性的不同变体,例如 xsi 似乎有两种不同的符号,例如 xsi:schemaLocation="..."xs:import schemaLocation="...".

And in some cases I see different variations of these elements/attributes, such as xsi which seems to have two different notations like xsi:schemaLocation="..." and xs:import schemaLocation="...".

我想在所有这些细微的变化之间,我似乎无法对每个变化做出正面或反面.在此先感谢您澄清这种混乱!

I guess between all these slight variations I can't seem to make heads or tails of what each of these does. Thanks in advance for bringing any clarity to this confusion!

推荐答案

您首先需要了解的是 XML 命名空间.如果您有一些时间可以浪费,您可以阅读规范.我发现这是与 XML 相关的更清晰的规范之一.如果你不明白它所说的一切,也没关系,这是一个很好的基础.但这里有一个快速的纲要.

The first thing you'll need to understand are XML namespaces. If you have some time to waste, you could read the specification. I found this to be one of the clearer specs related to XML. It doesn't matter if you don't understand everything it says, it's a good basis. But here's a quick rundown.

XML 元素和属性有一个名称.当您看到 <test att="hello"/> 时,您正在查看名称为test"的元素,其中我们有一个名称为att"的属性.但这并不是故事的全部...

XML elements and attributes have a name. When you see <test att="hello"/>, you're looking at an element with name "test", in which we have an attribute with name "att". But this isn't really the entire story...

XML 是一种允许您混合来自不同标记语言的内容的语法.例如,当使用 XSLT 将 XML 文档转换为 XHTML 页面时,您至少要处理在 XML 中定义的三种标记语言:输入文档的 XSLT 和 XHTML.如果每个人都保留自己的元素/属性名称并且不允许发生冲突,那么这种混合将变得相当困难.

XML is a syntax that allows you to mix content from different markup languages. For example, when using XSLT to turn an XML document into an XHTML page, you're dealing with at least three markup languages defined in XML: your input document's, XSLT and XHTML. Such mixes would become rather hard if each one reserved its own element/attribute names and no collisions were ever allowed.

输入 XML 命名空间.XML 命名空间定义了一个领域",其中元素和属性名称具有实际语义.元素模板"在 XSLT 名称空间中具有明确定义的含义.元素complexType"在 XML Schema 名称空间中具有明确定义的含义.如果您希望使用 XML 在您自己的标记语言中使用其中任何一种,那么只要您在不同的命名空间中这样做,那都是可能的.

Enter XML namespaces. An XML namespace defines a "sphere" within which element and attribute names have actual semantics. The element "template" has a well-defined meaning in the XSLT namespace. The element "complexType" has a well-defined meaning in the XML Schema namespace. If you wish to use either in your own markup language using XML, then that's possible provided you do so in a different namespace.

为了确保命名空间是唯一的,您需要提供一些唯一标识符.该规范决定使用 URI,通常采用 HTTP URL 的形式.原因很简单:这样的 URL 往往是很好的唯一标识符.但这也是一个非常常见的混淆原因,因为人们认为 URL 确实有意义,或者在 XML 处理过程中将通过网络访问.非常清楚事实并非如此! URL 不需要指向任何现有资源.它不会经过任何转换或解析为网络地址.即使两个 URL 指向完全相同的事物,一旦它们相差一个字符,它们就会被视为不同的命名空间.命名空间标识符只是一个字符串,并且区分大小写.仅此而已.

In order to make sure a namespace is unique, you'll need to provide some unique identifier. The specification settled on the use of URIs, most often in the form of a HTTP URL. The reason for this is simple: such URLs tend to be good unique identifiers. But it's also a very common cause for confusion because people think the URLs really hold meaning or will be accessed over the network during XML processing. Know very well that this is not the case! The URL is not required to point to any existing resource. It will not go through any transformation or be resolved to a network address. Even if two URLs would point to the exact same thing, the moment they differ by one character they are considered different namespaces. A namespace identifier is just a string, and a case-sensitive one at that. Nothing more.

随着命名空间的引入,XML 元素或属性的名称突然由两部分组成:命名空间和本地名称. 中的test"只是本地名称.所谓的完全限定名称"由命名空间和本地名称的一种无形组合组成.有时使用符号 {namespace URI}local-name ,但这只不过是约定.

With the introduction of namespaces, the name of an XML element or attribute suddenly consists of two parts: a namespace and a local name. That "test" in <test/> is only the local name. The so-called "fully-qualified name" consists of a kinda invisible combination of the namespace and the local name. Sometimes the notation {namespace URI}local-name is used, but that's nothing more than convention.

所以现在我们需要能够在 XML 文档中使用命名空间.为了声明命名空间,XML 具有硬编码机制.它使用特殊字符串 xmlns 来允许进行命名空间声明.可以通过以下两种方式之一完成:将命名空间绑定到前缀,或将其声明为默认命名空间.

So now we need to be able to use namespaces in an XML document. In order to declare a namespace, XML has a hard-coded mechanism. It uses the special string xmlns to allow namespace declarations to be made. It can be done in one of two ways: binding the namespace to a prefix, or declaring it as the default namespace.

绑定到前缀时,格式类似于:xmlns:prefix="namespace URI".这是 XML 文档中的示例:

When binding to a prefix, the form is something like this: xmlns:prefix="namespace URI". Here's an example in an XML document:

<foo:root xmlns:foo="http://www.foo.com">
    <foo:test />
</foo:root>

我们现在已经将名称空间 http://www.foo.com 绑定到前缀 foo.无论此前缀放在元素或属性名称前面的何处,我们都在声明它们是该命名空间的一部分.

We've now bound namespace http://www.foo.com to the prefix foo. Wherever this prefix is put in front of an element or attribute's name, we're stating that they're part of that namespace.

这里需要注意的是,实际前缀绝对没有任何意义.以下 XML 文档在语义上完全相同:

What's very important to note here is that the actual prefix means absolutely nothing. The following XML document is semantically the exact same:

<bar:root xmlns:bar="http://www.foo.com">
    <bar:test />
</bar:root>

前缀只是表示命名空间的一种方便方式.它使我们不必每次都完全使用 URI.

The prefix is merely a convenient way of representing the namespace. It saves us from having to use the URI entirely every time.

接下来是默认命名空间.可以使用 xmlns="namespace URI" 声明默认命名空间.您可以抽象地将其视为将命名空间绑定到空前缀.再次使用相同的 XML 文档,但这次没有前缀:

Next up is the default namespace. A default namespace can be declared with xmlns="namespace URI". You could abstractly think of this as binding a namespace to the empty prefix. Once again the same XML document, but this time without prefixes:

<root xmlns="http://www.foo.com">
    <test />
</root>

这样使用起来更方便一些.那么为什么有前缀呢?当我们混合来自不同命名空间的内容时,它们开始发挥作用:

This is a bit more convenient to work with. So why have prefixes at all? They start playing a role when we're mixing content from different namespaces:

<root xmlns="http://www.foo.com">
    <so:test xmlns:so="http://stackoverflow.com" />
</root>

这次是不同的 XML 文档.我们的 root 元素位于 http://www.foo.com 命名空间中,但 test 元素位于 http:///stackoverflow.com 因为我们已经绑定到 so 前缀并在 test 上使用它.

This time it's a different XML document. Our root element lies in the http://www.foo.com namespace, but the test element lies in http://stackoverflow.com because we've bound to so prefix to that and used it on test.

您还注意到,命名空间可以在 XML 文档中的任何元素上声明.该声明的范围(并绑定到前缀,如果适用)然后成为该元素及其内容.

You also notice here that namespaces can be declared on any element in the XML document. The scope of that declaration (and binding to prefix if applicable) then becomes that element and its content.

这有时会令人困惑,甚至更是如此,因为声明可能会相互覆盖.检查此文档:

This can sometimes become confusing, even more so since declarations may override each other. Check this document:

<root xmlns="http://www.foo.com">
    <test />
    <so:test xmlns:so="http://www.stackoverflow.com" xmlns="http://www.bar.com">
        <test />
    </so:test>
</root>

花点时间弄清楚每个元素所在的命名空间......这是一个很好的练习.

Take a moment and figure out what namespace each element is in... It's a good excercise.

root 位于名称空间 http://www.foo.com 中.第一个 test 元素也在该命名空间中,因为我们没有使用前缀但我们在该默认命名空间的范围内.带有前缀 so 的第二个 test 元素位于名称空间 http://www.stackoverflow.com 中,因为这是我们将前缀绑定到的内容.

root is in namespace http://www.foo.com. The first test element is also in that namespace, since we haven't used a prefix but we're in the scope of that default namespace. The second test element with prefix so lies in namespace http://www.stackoverflow.com because that's what we bound the prefix to.

然后是第三个,最里面的 test 元素.它在什么命名空间中?它没有前缀,所以它必须在默认命名空间中.但是,我们已经更改了第二个测试元素中的默认命名空间!所以现在最里面的元素属于 http://www.bar.com 命名空间,而不是 http://www.foo.com.

So then there's the third, innermost test element. What namespace is it in? It doesn't have a prefix, so it must be in the default namespace. BUT, we've changed our default namespace in the second test element! So now that innermost element belongs to the http://www.bar.com namespace, not http://www.foo.com.

还迷茫吗?请记住以下几点:

Confused yet? Just remember the following:

  • 命名空间只是一个字符串.URI 被用作拥有唯一标识符的便捷方式.
  • 前缀用于表示命名空间,但其名称没有任何意义.只需将其视为占位符即可.
  • 您可以设置默认命名空间.其范围内没有前缀的所有元素都将成为它的一部分.

呸.现在,进入 W3C XML Schema.所有这些与它有什么关系?

Phew. Now, onto W3C XML Schema. How does all of this relate to it?

嗯,对于初学者来说,XML Schema 本身是一种用 XML 定义的标记语言.所以它有自己的命名空间是理所当然的.该命名空间的正式名称是 http://www.w3.org/2001/XMLSchema.如果你把 S 写成小写,那就错了.开始明白为什么有些人真的讨厌命名空间?

Well, for starters XML Schema itself is a markup language defined in XML. So it stands to reason that it gets its own namespace. And that namespace is officially http://www.w3.org/2001/XMLSchema. If you write that S as lower case, it's wrong. Starting to see why some people really hate namespaces?

以下三个文件完全一样:

The following three documents are exactly the same:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</xsd:schema>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>

<schema xmlns="http://www.w3.org/2001/XMLSchema">
</schema>

重要的是我们使用了来自 XML Schema 命名空间的东西.然而,作为惯例,人们倾向于在 XML 模式中使用前缀 xsxsd.

All that matters is that we're using stuff from the XML Schema namespace. As a convention, however, people tend to use prefix xs or xsd in XML Schemas.

当我们有一个 XML 文档时,我们可能希望指定它的模式所在的位置.与 XML 文档相关的模式可能不止一种,因为正如我们所说的,语言可以混合在 XML 中.为了说明 XML 文档是模式的实例,再次提供了一个特殊的名称空间:http://www.w3.org/2001/XMLSchema-instance.按照惯例,我们倾向于将此命名空间绑定到前缀 xsi.但同样,这不是强制性的.

When we have an XML document, we may wish to specify where the schema(s) for it are located. More than one schema can be relevant for an XML document, because as we've stated languages can be mixed in XML. In order to say that an XML document is an instance of a schema, once again there's a special namespace available: http://www.w3.org/2001/XMLSchema-instance. By convention, we tend to bind this namespace to prefix xsi. But again, this isn't mandatory.

在该架构实例命名空间中定义了几个属性.其中包括schemaLocationnoNamespaceSchemaLocation.看看这个文件:

There's a couple of attributes defined in that schema instance namespace. Among them are schemaLocation and noNamespaceSchemaLocation. Take a look at this document:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.foo.com/schema">
</root>

那里发生了什么?首先,我们声明我们将前缀xsi 绑定到名称空间http://www.w3.org/2001/XMLSchema-instance.然后我们在该命名空间中使用了一个属性:noNamespaceSchemaLocation.该属性告诉我们模式位于何处以验证文档中不在任何特定命名空间中的那些部分.以下 XML 文档在语义上完全相同:

What happend there? First we stated that we're binding prefix xsi to the namespace http://www.w3.org/2001/XMLSchema-instance. Then we're used an attribute within that namespace: noNamespaceSchemaLocation. That attribute tells us where the schema is located to validate those parts of the document that aren't in any particular namespace. The following XML document is exactly the same, semantically:

<root xmlns:huh="http://www.w3.org/2001/XMLSchema-instance" huh:noNamespaceSchemaLocation="http://www.test.com/schema">
</root>

记住,前缀名称没有意义.它们是占位符.那么,noNamespaceSchemaLocation 属性是怎么回事?基本上,它告诉我们可以在哪里找到模式.现在与命名空间 URI 不同,这绝对是可用于从网络或本地存储中获取内容的东西.针对文档中声明的模式进行验证的 XML 处理器可能会尝试获取它.

Remember, prefix names have no meaning. They're placeholders. So, what's with that noNamespaceSchemaLocation attribute? Basically, it tells us where we can locate a schema. Now contrary to a namespace URI, this is most definitely something that can be used to fetch stuff from a network or local storage. An XML processor that validates against a schema declared in a document might try to obtain it.

还有一个事实,那就是它被称为 noNamespaceSchemaLocation.模式定义了目标命名空间".它的作用是说明它定义的元素和属性属于哪个命名空间.但是可以省略目标命名空间.在这种情况下,我们有一个没有命名空间的 XML 文档模式.可以使用 noNamespaceSchemaLocation 引用这样的模式.

Then there's the fact that it's called noNamespaceSchemaLocation. A schema defines a "target namespace". What this does is state what namespace the elements and attributes it defines are part of. But the target namespace may be omitted. In that case, we've got a schema for XML documents without namespace. Such a schema can be referred to with noNamespaceSchemaLocation.

在许多情况下,模式实际上会定义一个命名空间.为了说明哪个模式属于哪个命名空间,我们可以使用 http://www.w3.org/2001/XMLSchema-instance 命名空间中的另一个属性:schemaLocation.该属性可以包含命名空间 URI 和模式 URI 对(以空格分隔).假设我们有位于 http://www.myschemas.com/foo-schema 的命名空间 http://www.foo.com 的架构.那么我们可以这样表述:

In many cases, a schema will actually define a namespace. In order to say which schema belongs with which namespace, we can use another attribute from the http://www.w3.org/2001/XMLSchema-instance namespace: schemaLocation. That attribute can contain pairs (separated by spaces) of namespace URIs and schema URIs. Suppose we have schema for namespace http://www.foo.com located at http://www.myschemas.com/foo-schema. Then we can state that as follows:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.foo.com http://www.myschemas.com/foo-schema">
</root>

这是一个包含多个命名空间-位置对的示例:

Here's an example with multiple namespace-location pairs:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.foo.com http://www.myschemas.com/foo-schema http://www.bar.com http://www.randomschemas.com/bar-schema">
</root>

您需要记住的是,http://www.w3.org/2001/XMLSchema-instance 内容用于作为模式实例的 XML 文档.命名空间 http://www.w3.org/2001/XMLSchema 是用于定义架构本身的命名空间.

What you need to remember here is that http://www.w3.org/2001/XMLSchema-instance stuff is for use in XML documents that are instances of schemas. The namespace http://www.w3.org/2001/XMLSchema is the one used for defining schemas themselves.

所以现在我们已经在 URI 和具有特殊含义的奇怪属性中达到了我们的要求.这就是命名空间的问题:它们看起来非常复杂,直到您弄清楚它们是多么简单.只需密切关注哪个前缀绑定到哪个命名空间 URI,并知道该 URI 定义了什么.

So by now we're up to our neck in URIs and weird-looking attributes with special meanings. That's the thing with namespaces: they look very complex until you figure out how simple they are. Just keep a close eye on what prefix is bound to what namespace URI, and know what that URI defines.

对于您的问题,我还需要解决关于模式的另外两件事:xs:importxs:include.请注意我在这里如何使用 xs 前缀约定,因为我们讨论的是 W3C XML 模式.

There's two more things about schemas I need to address for your question: xs:import and xs:include. Notice how I've used the xs prefix convention here, since we're talking about W3C XML Schema.

include 元素可用于组合具有相同目标命名空间的模式.基本上它允许我们将模式模块化为更小的部分并将它们组合在一起.

The include element can be used to combine schemas with the same target namespace. Basically it allows us to modularize schemas into smaller parts and put them together.

import 元素在某种程度上是相同的,但对于具有不同目标命名空间的模式.这使我们可以组合不同标记语言的模式.

The import element does sort of the same, but for schemas with different target namespaces. This allows us to combine schemas for different markup languages.

总结一下:

  • xmlns:用于指定默认命名空间.
  • xmlns:prefix:用于将命名空间绑定到prefix.
  • http://www.w3.org/2001/XMLSchema:XML Schema 的命名空间.按照惯例,通常绑定到前缀 xs,但这不是强制性的,也不是自动完成的.
  • http://www.w3.org/2001/XMLSchema-instance:定义了一堆有用的东西的命名空间,用于声明 XML 文档如何成为模式实例的细节.按照惯例,通常绑定到前缀 xsi,但这不是强制性的,也不是自动完成的.
  • targetNamespace:可在 XML 模式(在根元素上)中使用的属性,用于指定这是哪个命名空间的模式定义.
  • schemaLocation:命名空间http://www.w3.org/2001/XMLSchema-instance定义的属性之一,用于指示一个或多个schema所在的位置可以找到一个或多个命名空间.
  • xmlns: used to specify a default namespace.
  • xmlns:prefix: used to bind a namespace to prefix.
  • http://www.w3.org/2001/XMLSchema: the namespace for XML Schema. By convention often bound to prefix xs, but this is not mandatory nor is it done automatically.
  • http://www.w3.org/2001/XMLSchema-instance: the namespace that defines a bunch of things useful for declaring the details of how an XML document is an instance of a schema. By convention often bound to prefix xsi, but this is not mandatory nor is it done automatically.
  • targetNamespace: an attribute that can be used in XML Schema (on the root element) to specify for which namespace this is a schema definition.
  • schemaLocation: one of the attributes defined by namespace http://www.w3.org/2001/XMLSchema-instance, used to indicate where one or more schemas can be found for one or more namespaces.

我的最后建议:找到一些方便的方法来根据模式验证文档并尝试一下.试验命名空间、包含和导入.使用多个命名空间制作文档并尝试确定范围.

My final advice: find some convenient way to validate documents against schemas and play around a bit. Experiment with namespaces, includes and imports. Make documents using multiple namespaces and try out the scoping.

之后,检查 XML 本身、XML 命名空间和 XML Schema 的规范.这是硬核阅读,但如果您通读它,您将了解到许多人在使用 XML 多年后似乎仍然错过了这一点.最终一切都会变得有意义.

After that, check the specifications of XML itself, XML namespaces and XML Schema. It's hardcore reading, but if you make your way through it you'll gain an understanding that many people still seem to miss after years of using XML. Eventually it'll all make sense.

祝你好运!

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

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