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

  • 相关阅读:
    MySQL基础语句【学习笔记】
    减一技术应用:生成排列与幂集
    Java实现动态规划法求解0/1背包问题
    LODOP中ADD_PRINT_TABLE、HTM、HTML表格自动分页测试
    LODOP设置判断后执行哪个
    Lodop删除语句Deleted只能内嵌设计维护可用
    Lodop、c-lodop注册与角色简短问答
    LODOP暂存、应用、复原 按钮的区别
    JS判断语句 注意多句时加大括号 回调函数LODOP兼顾写法
    LODOP、C-Lodop简短排查语句
  • 原文地址:https://www.cnblogs.com/jann8/p/13022536.html
Copyright © 2011-2022 走看看