是 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XML 中的特例? [英] Is xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" a special case in XML?

查看:58
本文介绍了是 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XML 中的特例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用命名空间时,我们还应该指出其关联的 XSD 所在的位置,如以下示例所示:

When we use a namespace, we should also indicate where its associated XSD is located at, as can be seen in the following example:

<?xml version="1.0"?>
<Artist BirthYear="1958" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.webucator.com/Artist"
 xsi:schemaLocation="http://www.webucator.com/Artist Artist.xsd">
 <Name>
  <Title>Mr.</Title>
  <FirstName>Michael</FirstName>
  <LastName>Jackson</LastName>
 </Name>
</Artist>

此处,我们已指出应使用 Artist.xsd 来验证 http://www.webucator.com/Artist 命名空间.但是,我们也使用了 http://www.w3.org/2001/XMLSchema-instance 命名空间,但是我们没有指定它的 XSD 所在的位置.XML 解析器如何知道如何处理这个命名空间?

Here, we have indicated that Artist.xsd should be used for validating the http://www.webucator.com/Artist namespace. However, we are also using the http://www.w3.org/2001/XMLSchema-instance namespace, but we have not specified where its XSD is located at. How do XML parsers know how to handle this namespace?

更新(回复第一个评论者)

那么,我们可以代替使用:

So, can we instead of using:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springmodules.org/schema/ehcache
            http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
...
</beans>

使用

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ehcache="http://www.springmodules.org/schema/ehcache">
...
</beans>

?

推荐答案

XML 解析器如何知道如何处理这个命名空间?

他们不会,除非他们这样做.基本思想是字符串 'http://www.w3.org/2001/XMLSchema-instance' 就像一个神奇的饼干.处理软件要么被编程为识别它,从而根据它的含义采取行动,要么没有.

They don't, except when they do. The basic idea is that the string 'http://www.w3.org/2001/XMLSchema-instance' works like a magic cookie. Either the processing software has been programmed to recognize it, and thus act on the basis of what it means, or it has not.

因此,仅仅通过识别的事实也带来了它所代表的知识":定义四个属性的命名空间"('type'、'nil'、'schemaLocation' 和'noNamespaceSchemaLocation')具有固定预定义的含义.

Thus, with the mere fact of recognition also comes the "knowledge" of what it represents: a "namespace" that defines four attributes ('type', 'nil', 'schemaLocation' and 'noNamespaceSchemaLocation') with fixed predefined meanings.

换句话说,如果您知道"字符串 'http://www.w3.org/2001/XMLSchema-instance'意味着",那么你也自动知道什么一个名为 xsi:schemaLocation 的属性意味着":它指向以W3C XML 模式"形式主义编码的模式文档.

In other words, if you "know" what the string 'http://www.w3.org/2001/XMLSchema-instance' "means", then you also automatically know what an attribute named xsi:schemaLocation "means": that it points to schema documents encoded in the 'W3C XML Schema' formalism.

这超出了 XML Namespaces Rec 实际提供的(其中只是对通用名称"或诸如此类的东西挥手致意).这里有一个约定,命名空间的语法(使用殖民名称)已被部署来硬编码语义理解:在哪里可以找到模式,在 W3C XML 模式的形式中,对于此文档实例."这一切都取决于对魔法饼干字符串的事先理解.

This goes beyond what the XML Namespaces Rec actually provides for (which is only some handwaving about "universal names" or whatnot). A convention is at work here, that the syntax of namespacing (using colonified names) has been deployed to hard-code a semantic understanding: "where to find the schema, in the formalism of W3C XML Schemas, for this document instance." It all hinges on prior understanding of that magic cookie string.

您可能会认为命名空间必须有一个架构,并且一台机器可以处理这个架构,并且只有在 W3C XML Schemas 形式主义中才能启动.这些都不一定是真的.存在其他模式形式(SGML/XML DTD、Relax-NG,与 W3C XML 模式不同,这两者都是国际标准);命名空间定义不必是机器可读的(它可以是散文,因为实际上它适用于 'http://www.w3.org/2001/XMLSchema-instance' 命名空间!);并且命名空间根本不需要正式定义,因为保证命名空间字符串所做的就是充当消歧标记.

You may be under the impression that a namespace must have a schema, and a machine processable one at that, and only in the W3C XML Schemas formalism to boot. None of these are necessarily true. Other schema formalisms exist (SGML/XML DTDs, Relax-NG, both of which, unlike W3C XML Schemas, are international standards); a namespace definition does not have to be machine-readable (it could be prose, as in fact it is for the 'http://www.w3.org/2001/XMLSchema-instance' namespace!); and a namespace need not be formally defined at all, because all a namespace string is guaranteed to do is to function as a disambiguation marker.

这篇关于是 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XML 中的特例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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