xml xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34202967/
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
xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?
提问by smwikipedia
For the following XML fragment:
对于以下 XML 片段:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
What do the xmlns, xmlns:xsi, and xsi:schemaLocationattributes exactly mean? How are they related? What's the :for?
做什么的xmlns,xmlns:xsi和xsi:schemaLocation属性究竟是什么意思?它们有什么关系?有什么:用?
And there are 2 URLs in the xsi:schemaLocation=
并且有 2 个网址 xsi:schemaLocation=
- http://maven.apache.org/POM/4.0.0(it happens to be the same as
xmlnsbut it gives a 404 error when visiting.) - http://maven.apache.org/xsd/maven-4.0.0.xsd(this is an actual XSD doc)
- http://maven.apache.org/POM/4.0.0(刚好是一样的,
xmlns但是访问时报404错误。) - http://maven.apache.org/xsd/maven-4.0.0.xsd(这是一个实际的 XSD 文档)
If 1 doesn't exist, why still put it there?
如果 1 不存在,为什么还要把它放在那里?
回答by kjhughes
Namespace related attributes in XML and XML Schema (XSD)
XML 和 XML Schema (XSD) 中的命名空间相关属性
xmlnsis part of the W3C Namespaces in XML Recommendation:The prefix xmlnsis used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/.
In your example, it declares that
http://maven.apache.org/POM/4.0.0is the default namespace for the elements in your Maven project.xmlns:xsideclares a standard namespace prefix (xsi) for a core namespace used in XSD:http://www.w3.org/2001/XMLSchema-instanceXML Schema: Structuresalso defines several attributes for direct use in any XML documents. These attributes are in a different namespace, which has the namespace name http://www.w3.org/2001/XMLSchema-instance. For brevity, the text and examples in this specification use the prefix xsi: to stand for this latter namespace; in practice, any prefix can be used.
In your example, it declares the conventional binding of the
xsinamespace prefix tohttp://www.w3.org/2001/XMLSchema-instance, which properly sets up the use of the following attributes:xsi:typeallows an XML instance to associate element type information directly rather than through an XSD. See How to restrict the value of an XML element using xsi:type in XSD?In your example,
xsi:typeis not used; included here for completeness regardingxsi.xsi:nilallows an empty element to be considered to be valid when the XSD might not otherwise have allowed it.In your example,
xsi:nilis not used; included here for completeness regardingxsi.xsi:schemaLocationandxsi:noNamespaceSchemaLocationprovide hints to the XML processor as to how to associate an XSD with an XML document. Usexsi:schemaLocationwhen there is a namespace; usexsi:noNamespaceSchemaLocationwhen there is no namespace.In your example, there is a namespace, so you properly use
xsi:schemaLocation, whose values are space-separated pairsof namespaceand XSD-location-URI. Your example uses the namespace,http://maven.apache.org/POM/4.0.0, and namespaces are lexical naming constructs that need not be retrivable. Your example also uses the XSD-location-URI,http://maven.apache.org/xsd/maven-4.0.0.xsd, which is retrivable as it should be.If your example did not use a namespace, you would use
xsi:noNamespaceSchemaLocation, whose value is a single XSD-location-URIthat hints to the location of the intended XSD and which should be retrievable.
targetNamespaceis an attribute on thexs:schemaroot element of an XSD which specifies the namespace of the root element of the XML document instances the XSD is intended to govern. It must match the default or explicit namespace of those XML documents' root elements.
xmlns是W3C Namespaces in XML Recommendation 的一部分:前缀xmlns仅用于声明名称空间绑定,并且根据定义绑定到名称空间名称 http://www.w3.org/2000/xmlns/。
在您的示例中,它声明这
http://maven.apache.org/POM/4.0.0是 Maven 项目中元素的默认命名空间。xmlns:xsixsi为 XSD 中使用的核心命名空间声明标准命名空间前缀 ( ):http://www.w3.org/2001/XMLSchema-instanceXML Schema:Structures还定义了几个属性,可在任何 XML 文档中直接使用。这些属性位于不同的命名空间中,命名空间名称为 http://www.w3.org/2001/XMLSchema-instance。为简洁起见,本规范中的文本和示例使用前缀 xsi: 代表后一个命名空间;实际上,可以使用任何前缀。
在您的示例中,它将
xsi命名空间前缀的常规绑定声明为http://www.w3.org/2001/XMLSchema-instance,从而正确设置了以下属性的使用:xsi:type允许 XML 实例直接关联元素类型信息,而不是通过 XSD。请参阅如何在 XSD 中使用 xsi:type 限制 XML 元素的值?在您的示例中,
xsi:type未使用;包括在这里是为了完整性xsi。xsi:nil当 XSD 可能不允许时,允许将空元素视为有效。在您的示例中,
xsi:nil未使用;包括在这里是为了完整性xsi。xsi:schemaLocation并xsi:noNamespaceSchemaLocation提供提示,以XML处理器为如何将XSD与XML文档相关联。xsi:schemaLocation有命名空间时使用;xsi:noNamespaceSchemaLocation在没有命名空间时使用。在您的示例中,有一个名称空间,因此您可以正确使用
xsi:schemaLocation,其值是以空格分隔的 名称空间对和XSD-location-URI。您的示例使用命名空间,和命名空间是不需要可检索的词法命名构造。您的示例还使用了 XSD-location-URI, ,它应该是可检索的。http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd如果您的示例未使用命名空间,您将使用
xsi:noNamespaceSchemaLocation,其值为单个XSD-location-URI,提示预期 XSD 的位置并且应该是可检索的。
targetNamespace是xs:schemaXSD 根元素上的一个属性,它指定 XSD 打算管理的 XML 文档实例的根元素的名称空间。它必须与这些 XML 文档的根元素的默认名称空间或显式名称空间相匹配。
回答by Nodonutsforyou
xmlnsdefines default namespace, which states, that all nodes within project node and without an namespace-alias will be in http://maven.apache.org/POM/4.0.0namespace by default.
xmlns定义默认命名空间,它规定项目节点中的所有节点和没有命名空间别名的节点http://maven.apache.org/POM/4.0.0默认都在命名空间中。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"defines namespace - http://www.w3.org/2001/XMLSchema-instance, and gives it a new alias - xsi
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"定义命名空间 - http://www.w3.org/2001/XMLSchema-instance,并给它一个新的别名 -xsi
xsi:schemaLocationis an attribute schemaLocationof "http://www.w3.org/2001/XMLSchema-instance"namespace. It contains pairs of values - namespace URI and schema location link for xsd-schema file of that namespace. It can contain many pairs of values - one xsd file for every defined namespace URI. That means link http://maven.apache.org/xsd/maven-4.0.0.xsdcontains xsd schema with definition of http://maven.apache.org/POM/4.0.0namespace.
xsi:schemaLocation是一个属性schemaLocation的"http://www.w3.org/2001/XMLSchema-instance"名称空间。它包含值对 - 命名空间 URI 和该命名空间的 xsd-schema 文件的架构位置链接。它可以包含多对值 - 每个定义的命名空间 URI 都有一个 xsd 文件。这意味着链接http://maven.apache.org/xsd/maven-4.0.0.xsd包含具有http://maven.apache.org/POM/4.0.0命名空间定义的 xsd 模式。

