xmlns,xmlns:xsi,xsi:schemaLocation和targetNamespace?

对于以下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">

xmlns,xmlns:xsi和xsi:schemaLocation属性到底意味着什么?它们有什么关系?什么是:为了?

xsi中有2个URL:schemaLocation =

> http://maven.apache.org/POM/4.0.0(它恰好与xmlns相同,但在访问时会出现404错误。)
> http://maven.apache.org/xsd/maven-4.0.0.xsd(这是一个实际的XSD文档)

如果1不存在,为什么还要把它放在那里?

XML和XML架构(XSD)中与命名空间相关的属性

> xmlnsW3C Namespaces in XML Recommendation的一部分:

The prefix xmlns is used only to declare namespace bindings and
is by deFinition bound to the namespace name
07002.

在您的示例中,它声明http://maven.apache.org/POM/4.0.0是Maven项目中元素的认命名空间。
> xmlns:xsi为XSD中使用的核心命名空间声明标准名称空间前缀(xsi):http://www.w3.org/2001/XMLSchema-instance

XML Schema: Structures also defines several attributes for direct use
in any XML documents. These attributes are in a different namespace,
which has the namespace name
07004. 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.

在您的示例中,它声明了xsi名称空间前缀与http://www.w3.org/2001/XMLSchema-instance的常规绑定,它正确设置了以下属性的使用:

> xsi:type允许XML实例直接关联元素类型信息,而不是通过XSD。见How to restrict the value of an XML element using xsi:type in XSD?

在您的示例中,未使用xsi:type;关于xsi的完整性包括在这里
> xsi:nil允许在XSD可能不允许的情况下将空元素视为有效。

在您的示例中,未使用xsi:nil;关于xsi的完整性包括在这里
> xsi:schemaLocation and xsi:noNamespaceSchemaLocation向XML处理器提供有关如何将XSD与XML文档相关联的提示。有命名空间时使用xsi:schemaLocation;没有命名空间时使用xsi:noNamespaceSchemaLocation。

在您的示例中,有一个名称空间,因此您可以正确使用xsi:schemaLocation,其值是空格分隔的名称空间对和XSD-location-URI。您的示例使用名称空间http://maven.apache.org/POM/4.0.0,名称空间是need not be retrivable的词法命名结构。您的示例还使用XSD-location-URI,http://maven.apache。 org / xsd / maven-4.0.0.xsd,它应该是可以恢复的。

如果您的示例未使用命名空间,则应使用xsi:noNamespaceSchemaLocation,其值为单个XSD-location-URI,提示指向预期XSD的位置以及哪些应该是可检索的。

> targetNamespace是xs:schema root上的属性XSD的元素,它指定根元素的名称空间XSD旨在管理的XML文档实例。它必须匹配这些XML文档的root的认或显式命名空间元素。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念