简体   繁体   English

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

[英]xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

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:schemaLocation attributes exactly mean? xmlnsxmlns:xsixsi:schemaLocation属性到底意味着什么? How are they related? 它们有什么关系? What's the : for? 什么是:为了?

And there are 2 URLs in the xsi:schemaLocation= xsi:schemaLocation=有2个URL xsi:schemaLocation=

  1. http://maven.apache.org/POM/4.0.0 (it happens to be the same as xmlns but it gives a 404 error when visiting.) http://maven.apache.org/POM/4.0.0 (它恰好与xmlns相同,但访问时会出现404错误。)
  2. http://maven.apache.org/xsd/maven-4.0.0.xsd (this is an actual XSD doc) http://maven.apache.org/xsd/maven-4.0.0.xsd (这是一个实际的XSD文档)

If 1 doesn't exist, why still put it there? 如果1不存在,为什么还要把它放在那里?

Namespace related attributes in XML and XML Schema (XSD) XML和XML架构(XSD)中与命名空间相关的属性

  • xmlns is part of the W3C Namespaces in XML Recommendation : xmlnsXML建议书中W3C命名空间的一部分:

    The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/ . 前缀xmlns仅用于声明名称空间绑定,并且根据定义绑定到名称空间名称http://www.w3.org/2000/xmlns/

    In your example , it declares that http://maven.apache.org/POM/4.0.0 is the default namespace for the elements in your Maven project. 在您的示例中 ,它声明http://maven.apache.org/POM/4.0.0是Maven项目中元素的默认命名空间。

  • xmlns:xsi declares a standard namespace prefix ( xsi ) for a core namespace used in XSD: http://www.w3.org/2001/XMLSchema-instance xmlns:xsi为XSD中使用的核心命名空间声明标准名称空间前缀( xsi ): http://www.w3.org/2001/XMLSchema-instancehttp://www.w3.org/2001/XMLSchema-instance

    XML Schema: Structures also defines several attributes for direct use in any XML documents. XML Schema:Structures还定义了几个可直接用于任何XML文档的属性。 These attributes are in a different namespace, which has the namespace name http://www.w3.org/2001/XMLSchema-instance . 这些属性位于不同的命名空间中,其名称空间名称为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; 为简洁起见,本规范中的文本和示例使用前缀xsi:代表后一个命名空间; in practice, any prefix can be used. 实际上,可以使用任何前缀。

    In your example , it declares the conventional binding of the xsi namespace prefix to http://www.w3.org/2001/XMLSchema-instance , which properly sets up the use of the following attributes: 在您的示例中 ,它声明了xsi名称空间前缀与http://www.w3.org/2001/XMLSchema-instance的常规绑定,它正确设置了以下属性的使用:

    • xsi:type allows an XML instance to associate element type information directly rather than through an XSD. xsi:type允许XML实例直接关联元素类型信息,而不是通过XSD。 See How to restrict the value of an XML element using xsi:type in XSD? 请参阅如何在XSD中使用xsi:type限制XML元素的值?

      In your example , xsi:type is not used; 在您的示例中 ,未使用xsi:type ; included here for completeness regarding xsi . 关于xsi完整性包括在这里。

    • xsi:nil allows an empty element to be considered to be valid when the XSD might not otherwise have allowed it. xsi:nil允许在XSD可能不允许的情况下将空元素视为有效。

      In your example , xsi:nil is not used; 在您的示例中 ,未使用xsi:nil ; included here for completeness regarding xsi . 关于xsi完整性包括在这里。

    • xsi:schemaLocation and xsi:noNamespaceSchemaLocation provide hints to the XML processor as to how to associate an XSD with an XML document. xsi:schemaLocationxsi:noNamespaceSchemaLocation向XML处理器提供有关如何将XSD与XML文档关联的提示。 Use xsi:schemaLocation when there is a namespace; 有命名空间时使用xsi:schemaLocation ; use xsi:noNamespaceSchemaLocation when there is no namespace. 没有命名空间时使用xsi:noNamespaceSchemaLocation

      In your example , there is a namespace, so you properly use xsi:schemaLocation , whose values are space-separated pairs of namespace and XSD-location-URI . 在您的示例中 ,有一个名称空间,因此您可以正确使用xsi:schemaLocation ,其值是空格分隔的名称空间 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 . 您的示例使用名称空间http://maven.apache.org/POM/4.0.0 ,名称空间是不需要可重复使用的词法命名结构。 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. 您的示例还使用了XSD-location-URI, http://maven.apache.org/xsd/maven-4.0.0.xsd ://maven.apache.org/xsd/maven-4.0.0.xsd,它应该可以自行调整。

      If your example did not use a namespace, you would use xsi:noNamespaceSchemaLocation , whose value is a single XSD-location-URI that hints to the location of the intended XSD and which should be retrievable. 如果您的示例未使用命名空间,则应使用xsi:noNamespaceSchemaLocation ,其值为单个XSD-location-URI ,该URI提示预期XSD的位置以及哪些应该是可检索的。

  • targetNamespace is an attribute on the xs:schema root element of an XSD which specifies the namespace of the root element of the XML document instances the XSD is intended to govern. targetNamespace是XSD的xs:schema根元素上的一个属性,它指定XSD要管理的XML文档实例的根元素的名称空间。 It must match the default or explicit namespace of those XML documents' root elements. 它必须与这些XML文档的根元素的默认或显式命名空间匹配。

xmlns defines 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.0 namespace 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:schemaLocation is an attribute schemaLocation of "http://www.w3.org/2001/XMLSchema-instance" namespace. xsi:schemaLocation"http://www.w3.org/2001/XMLSchema-instance"命名空间的属性schemaLocation It contains pairs of values - namespace URI and schema location link for xsd-schema file of that namespace. 它包含一对值 - 名称空间URI和该名称空间的xsd-schema文件的模式位置链接。 It can contain many pairs of values - one xsd file for every defined namespace URI. 它可以包含许多值对 - 每个已定义的名称空间URI都有一个xsd文件。 That means link http://maven.apache.org/xsd/maven-4.0.0.xsd contains xsd schema with definition of http://maven.apache.org/POM/4.0.0 namespace. 这意味着链接http://maven.apache.org/xsd/maven-4.0.0.xsd包含带有http://maven.apache.org/POM/4.0.0命名空间定义的xsd模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM