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

  • 相关阅读:
    Android ClearEditText:输入用户名、密码错误时整体删除及输入为空时候晃动提示
    Activity界面切换动画特效。
    点击事件的确认取消对话框。
    安卓菜单的实现,各种添加菜单的方法。
    联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
    获取手机屏幕密度。
    Android统计图表MPAndroidChart.
    性能测试
    自动化框架
    排序算法
  • 原文地址:https://www.cnblogs.com/jann8/p/13022536.html
Copyright © 2020-2023  润新知