xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 是 XML 中的特例吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2615892/
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
Is xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" a special case in XML?
提问by Behrang Saeedzadeh
When we use a namespace, we should also indicate where its associated XSD is located at, as can be seen in the following example:
当我们使用命名空间时,我们还应该指出其关联的 XSD 所在的位置,如以下示例所示:
<?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>Hymanson</LastName>
</Name>
</Artist>
Here, we have indicated that Artist.xsd should be used for validating the http://www.webucator.com/Artistnamespace. However, we are also using the http://www.w3.org/2001/XMLSchema-instancenamespace, but we have not specified where its XSD is located at. How do XML parsers know how to handle this namespace?
在这里,我们已经指出应该使用 Artist.xsd 来验证http://www.webucator.com/Artist命名空间。然而,我们也在使用http://www.w3.org/2001/XMLSchema-instance命名空间,但我们没有指定它的 XSD 位于何处。XML 解析器如何知道如何处理这个命名空间?
Update (in response to the first commenter)
更新(响应第一个评论者)
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>
use
用
<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>
?
?
回答by arayq2
How do XML parsers know how to handle this namespace?
XML 解析器如何知道如何处理这个命名空间?
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.
他们不这样做,除非他们这样做。基本思想是字符串 ' http://www.w3.org/2001/XMLSchema-instance' 就像一个魔法饼干。处理软件要么被编程为识别它,从而根据它的含义采取行动,要么没有。
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 fixedpredefined meanings.
因此,仅仅识别的事实也带来了它所代表的“知识”:定义四个属性(“type”、“nil”、“schemaLocation”和“noNamespaceSchemaLocation”)的“命名空间”,具有固定的预定义含义。
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.
换句话说,如果您“知道”字符串 ' http://www.w3.org/2001/XMLSchema-instance' “意味着”什么,那么您也自动知道名为 xsi:schemaLocation 的属性“意味着”什么:它指向以“W3C XML 模式”形式主义编码的模式文档。
This goes beyond what the XML Namespaces Recactually provides for (which is only some handwaving about "universal names" or whatnot). A convention is at work here, that the syntaxof 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.
这超出了XML Namespaces Rec实际提供的范围(这只是一些关于“通用名称”或诸如此类的东西)。这里有一个约定,命名空间的语法(使用殖民名称)已被部署到硬编码语义理解:“在 W3C XML 模式的形式中,为这个文档实例在哪里找到模式。” 这一切都取决于对魔术曲奇串的事先理解。
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.
您可能会认为命名空间必须有一个模式,并且一台机器可以处理这个模式,并且只能在 W3C XML Schemas 形式主义中启动。这些都不一定是真的。存在其他模式形式(SGML/XML DTD、Relax-NG,与 W3C XML 模式不同,两者都是国际标准);命名空间定义不必是机器可读的(它可以是散文,因为实际上它适用于“ http://www.w3.org/2001/XMLSchema-instance”命名空间!);并且命名空间根本不需要正式定义,因为保证命名空间字符串所做的就是充当消歧标记。
回答by John Saunders
There's no requirement to say where the schema is located. You can do it if you want, but you don't have to.
无需说明架构位于何处。如果您愿意,您可以这样做,但您不必这样做。
In this example, all platforms are likely to understand where the schemas for xsi, xml, xsdand soapare all located.
在这个例子中,所有平台都可能明白的地方的模式xsi,xml,xsd并soap都位于。
EDIT:Like I said, all platforms are likely to know where the schemas are for these well-known namespaces. Quite likely, they all have copies of the schemas. I use Visual Studio, and it keeps copies of these schemas online, and refers to them as necessary.
编辑:就像我说的,所有平台都可能知道这些众所周知的命名空间的架构在哪里。很可能,他们都有模式的副本。我使用 Visual Studio,它在线保留这些模式的副本,并在必要时引用它们。
回答by dkackman
There are four built in declarations for Xml schemata; type, nil, schemaLocationand noNamespaceSchemaLocationas are "present in every schema by definition." You can read about them in the Xml Schema recommendation.
Xml 模式有四个内置声明;type,nil,schemaLocation并且noNamespaceSchemaLocation因为是“目前在定义每一个架构。” 您可以在Xml Schema 推荐中阅读有关它们的信息。
回答by gavenkoa
If you look to content of: http://www.w3.org/2001/XMLSchema-instanceyou found that there is text:
如果您查看以下内容:http: //www.w3.org/2001/XMLSchema-instance,您会发现有文字:
...
<xs:schema targetNamespace="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/1999/xhtml">
<xs:annotation>
<xs:documentation>
<h1>XML Schema instance namespace</h1>
<p>See <a href="http://www.w3.org/TR/xmlschema-1/">the XML Schema
Recommendation</a> for an introduction</p>
...
Because xmlns="http://www.w3.org/1999/xhtml"you see above link as HTML document.
因为xmlns="http://www.w3.org/1999/xhtml"您将上面的链接视为 HTML 文档。
According to http://www.w3.org/TR/xmlschema-1/#schema-loc(Schema Representation Constraint: Schema Document Location Strategy) schema-aware processors may implement any combination of the following strategies, in any order:
根据http://www.w3.org/TR/xmlschema-1/#schema-loc(Schema Representation Constraint: Schema Document Location Strategy)模式感知处理器可以以任何顺序实现以下策略的任意组合:
- Attempt to resolve the namespace name to locate such a resource.
- 尝试解析命名空间名称以定位这样的资源。
So http://www.w3.org/2001/XMLSchema-instancepoint to itself (yea, recursion!!).
所以http://www.w3.org/2001/XMLSchema-instance指向它自己(是的,递归!!)。
See similar answer: Where is the XSD file for "http://www.w3.org/2001/XMLSchema-instance"?
请参阅类似的答案:“http://www.w3.org/2001/XMLSchema-instance”的 XSD 文件在哪里?
PS. Any specialized XML processor can treat http://www.w3.org/2001/XMLSchema-instanceas known thing without actually download this definition (and most do that).
附注。任何专门的 XML 处理器都可以将其http://www.w3.org/2001/XMLSchema-instance视为已知的东西,而无需实际下载此定义(大多数都这样做)。
PPS. Semantic of http://www.w3.org/2001/XMLSchema-instancedefined by XML Schemastandard http://www.w3.org/TR/xmlschema-1/
聚苯乙烯。http://www.w3.org/2001/XMLSchema-instance由XML Schema标准定义的语义 http://www.w3.org/TR/xmlschema-1/

