zoukankan      html  css  js  c++  java
  • xml中xsd、xsi、xmlns的含义

        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" >

  • 相关阅读:
    软件工程第一次作业
    7.9-7.22日团队进展及博客更新状况
    2018 软件工程暑期培训小结
    结对编程(第7组)
    6.3日前团队项目打分情况
    第六周实践作业:软件测试和评估
    第4周小组作业:WordCount优化
    第2周个人作业:WordCount
    博客阅读和思考
    个人总结
  • 原文地址:https://www.cnblogs.com/jann8/p/13022536.html
Copyright © 2011-2022 走看看