码迷,mamicode.com
首页 > 其他好文 > 详细

xml中xsd、xsi、xmlns的含义

时间:2020-06-01 00:57:18      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:instance   ica   文件的   conf   structs   表示   version   mave   data   

    Maven的pom.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为前缀的标签属性的命名空间,xsi:schemaLocation就是该命名空间下的属性;
              xsi:schemaLocation的值是xml文件书写需要遵循的xsd约束,它包括两部分:前面是xsd文件对应的命名空间,后面是xsd文件的地址。

    打开地址http://maven.apache.org/xsd/maven-4.0.0.xsd"包含如下头信息:
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                            xmlns="http://maven.apache.org/POM/4.0.0" 
                            elementFormDefault="qualified" 
                            targetNamespace="http://maven.apache.org/POM/4.0.0">
     targetNamespace表示该xsd只对该目标命名空间生效,即xsd文件对应该命名空间。在pom.xml中使用xsi:schemaLocation引入xsd文件的时候,在xsd文件的前面就是该xsd对应的命名空间。

    在一个xml文件中可以定义多个命名空间,比如在springmvc-servlet.xml文件中,它的文件头信息如下:
       <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:mvc="http://www.springframework.org/schema/mvc"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:context="http://www.springframework.org/schema/context"
                 xsi:schemaLocation="
                         http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.springframework.org/schema/mvc
                         http://www.springframework.org/schema/mvc/spring-mvc.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context.xsd">
     springmvc-servlet.xml的每个命名空间对应不同的xsd约束文件。如图1,以命名空间为前缀的标签/属性受该命名空间对应的xsd文件约束。

技术图片

图1 spring-servlet.xml中的多个命名空间

    再回到pom.xml文件中。xml文件中头信息中包含的xmlns:xsi可以看出,该xml文件可以看成是受xml-schema-instance的约束,xml-schema-instance就是xsd。在http://maven.apache.org/xsd/maven-4.0.0.xsd中,文件头信息xmlns:xs="http://www.w3.org/2001/XMLSchema"表明xsd文件受xml-schema文件约束。如下图所示:
    xml-Schema → xml-schema-instance(xsd)→ xml       注: “→“”表示约束关系

    这在“https://www.w3.org/TR/xmlschema-1/#ref-xml-namespaces”里也有描述:
       The purpose of XML Schema: Structures is to define the nature of XML schemas and their component parts, provide an inventory of XML markup constructs with which to represent schemas, and define the application of schemas to XML documents.
       The purpose of an XML Schema: Structures schema is to define and describe a class of XML documents by using schema components to constrain and document the meaning, usage and relationships of their constituent parts: datatypes, elements and their content and attributes and their values. Schemas may also provide for the specification of additional document information, such as normalization and defaulting of attribute and element values. Schemas have facilities for self-documentation. Thus, XML Schema: Structures can be used to define, describe and catalogue XML vocabularies for classes of XML documents.

    xml的约束有两种方式,除了上面提到的xsd,还有dtd约束文件。在mybatis-config.xml中就使用的dtd约束,它的文件头信息如下:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >

xml中xsd、xsi、xmlns的含义

标签:instance   ica   文件的   conf   structs   表示   version   mave   data   

原文地址:https://www.cnblogs.com/jann8/p/13022536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!