• 关于XML的Schema文件讲解


    1 Schema概述

    1.1 什么是Schema

    Schema是新的XML文档约束;DTD出现的比较早.

    l  Schema要比DTD强大很多;

    Schema本身也是XML文档,但Schema文档的扩展名为xsd,而不是xml。( XML Schemas Definition )(xmlns : XML namespace)

    1.2 Schema简介

      本课程中不对Schema深入探讨,我们只对Schema有个了解即可。

    students.xsd

    <?xml version="1.0"?>
    <xsd:schema xmlns="http://www.itcast.cn/xml"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.itcast.cn/xml"  elementFormDefault="qualified"> 指定目标名称空间名称
        <xsd:element name="students" type="studentsType"/> 定义students元素,类型为studentsType类型
        <xsd:complexType name="studentsType"> 定义studentsType类型
            <xsd:sequence>
                <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/> studentsType类型包含0~N个student元素
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="studentType"> 定义studentType类型
            <xsd:sequence> 子元素必须按顺序出现
                <xsd:element name="name" type="xsd:string"/> studentType类型顺序包含子元素name,类型为字符串
                <xsd:element name="age" type="ageType" /> studentType类型顺序包含子元素age,类型为ageType
                <xsd:element name="sex" type="sexType" /> studentType类型顺序包含子元素sex,类型为sexType
            </xsd:sequence>
            <xsd:attribute name="number" type="numberType" use="required"/> studentType类型包含属性number,类型为numberType,该属性是必须的
        </xsd:complexType>
        <xsd:simpleType name="sexType"> 定义类型sexType
            <xsd:restriction base="xsd:string"> 该类型是对string进行约束
                <xsd:enumeration value="male"/> 指定枚举选项
                <xsd:enumeration value="female"/> 指定枚举选项
            </xsd:restriction>
        </xsd:simpleType>
        <xsd:simpleType name="ageType"> 定义ageType类型
            <xsd:restriction base="xsd:integer"> 该类型对integer进行约束
                <xsd:minInclusive value="0"/>
                <xsd:maxInclusive value="120"/> 范围为0~120
            </xsd:restriction>
        </xsd:simpleType>
        <xsd:simpleType name="numberType"> 定义numberType类型
            <xsd:restriction base="xsd:string"> 对string类型进行约束
                <xsd:pattern value="ITCAST_d{4}"/> 指定正则表达式
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:schema> 

    students.xml

    <?xml version="1.0"?>
     <students xmlns="http://www.itcast.cn/xml" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.itcast.cn/xml students.xsd">指定默认名称空间,指定Schema约束文件
        <student number="ITCAST_1001"> 子元素student,其中number属性是必须的,而且number属性值必须匹配正则表达式ITCAST_d{4}
            <name>zhangSan</name> 子元素name,内容为任意字符串
            <age>20</age> 子元素age,内容必须是0~100之间的整数
            <sex>male</sex> 子元素sex,内容必须是male和female其中之一。
        </student>
        <student number="ITCAST_1002">
            <name>liSi</name>
            <age>25</age>
            <sex>female</sex>
        </student>
    </students> 
    2 Schema名称空间

    2.1 什么是名称空间

    如果一个XML文档中使用多个Schema文件,而这些Schema文件中定义了相同名称的元素时就会出现名字冲突。这就像一个Java文件中使用了import java.util.*和import java.sql.*时,在使用Date类时,那么就不明确Date是哪个包下的Date了。

    总之名称空间就是用来处理元素和属性的名称冲突问题,与Java中的包是同一用途。如果每个元素和属性都有自己的名称空间,那么就不会出现名字冲突问题,就像是每个类都有自己所在的包一样,那么类名就不会出现冲突。

    2.2 目标名称空间

    在XSD文件中为定义的元素指定名称,即指定目标名称空间。这需要给<xsd:schema>元素添加targetNamespace属性。

    l  <xsd:schema targetNamespace="http://www.itcast.cn/xml">

    名称空间可以是任意字符串,但通常我们会使用公司的域名作为名称空间,这与Java中的包名使用域名的倒序是一样的!千万不要以为这个域名是真实的,它可以是不存在的域名

    如果每个公司发布的Schema都随意指定名称空间,如a、b之类的,那么很可能会出现名称空间的名字冲突,所以还是使用域名比较安全,因为域名是唯一的。

    当使用了targetNamespace指定目标名称空间后,那么当前XSD文件中定义的元素和属性就在这个名称空间之中了。

    2.3 XML指定XSD文件

    在XML文件中需要指定XSD约束文件,这需要使用在根元素中使用schemaLocation属性来指定XSD文件的路径,以及目标名称空间。格式为:schemaLocation=”目标名称空间 XSD文件路径”

    <students  schemaLocation="http://www.itcast.cn/xml students.xsd">

    schemaLocation是用来指定XSD文件的路径,也就是说为当前XML文档指定约束文件。但它不只要指定XSD文件的位置,还要指定XSD文件的目标名称空间。

    其中http://www.itcast.cn/xml为目标名称空间,students.xsd为XSD文件的位置,它们中间使用空白符(空格或换行)分隔。

    也可以指定多个XSD文件,格式为:

    l  schemaLocation=”目标名称空间1 XSD文件路径1 目标名称空间2 XSD文件路径2”

    下面是spring配置文件的例子,它一共指定两个XSD文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xsi:schemaLocation="http://www.springframework.org/schema/beans  第一个XSD文件的目标名称空间
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  第一个xsd文件的位置,这个网址必须是真实的,不然就找不到xsd文件了.
                          http://www.springframework.org/schema/aop   第二个xsd文件的目标名称空间
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 第二个xsd文件的位置.
    </beans> 

    xsi:schemaLocation="http://www.springframework.org/schema/beans    第一个XSD文件的目标名称空间

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    第一个XSD文件的位置,这个网址必须是真实的,不然就找不到XSD文件了。

    http://www.springframework.org/schema/aop   第二个XSD文件的目标名称空间

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    第二个XSD文件的位置

    下面是JavaWeb项目的配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    </web-app> 

    2.4 定义名称空间

    现在我们已经知道一个XML中可以指定多个XSD文件,例如上面Spring的配置文件中就指定了多个XSD文件,那么如果我在<beans>元素中给出一个子元素<bean>,你知道它是哪个名称空间中的么?

    <beans
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                          http://www.springframework.org/schema/aop 
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean></bean> 
    </beans>

    如果两个名称空间中都<bean>元素会怎么样呢?

    所以只是使用schemaLocation指定XSD是不够的,它只是导入了这个XSD及XSD的名称空间而已。schemaLocation的作用就相当于Java中导入Jar包的作用!最终还是在Java文件中使用import来指定包名的。

    xmlns是用来指定名称空间前缀的,所谓前缀就是“简称”,例如中华人发共和国简称中国一样,然后我们在每个元素前面加上前缀,就可以处理名字冲突了。

    格式为:xmln:前缀=”名称空间”

    注意,使用xmlns指定的名称空间必须是在schemaLocation中存在的名称空间。

    <beans
    xmlns:b="http://www.springframework.org/schema/beans" 指定b前缀
    xmlns:aop="http://www.springframework.org/schema/aop" 指定aop前缀
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                          http://www.springframework.org/schema/aop 
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      <b:bean></b:bean> 
    在使用元素时,需指定前缀,这就可以区别元素是哪个名称空间下的了。每个元素都要指定名称空间前缀,如果不指定那么元素就是没有名称空间。 <aop:scoped-proxy/> </beans>

    2.5 默认名称空间

    在一个XML文件中,可以指定一个名称空间没有前缀,那么在当前XML文档中没有前缀的元素就来自默认名称空间。

    <beans
     xmlns="http://www.springframework.org/schema/beans" 
    指定默认名称空间,即没有前缀的名称空间,如果元素没有指定前缀,那么表示来自这个名称空间 xmlns:aop
    ="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean></bean>
    来自默认名称空间,因为没有前缀。 <aop:scoped-proxy/> </beans>

    2.6 W3C的元素和属性

    如果我们的XML文件中需要使用W3C提供的元素和属性,那么可以不在schemaLocation属性中指定XSD文件的位置,但一定要定义名称空间,例如:

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 定义名称控件,这个名称空间来自w3c所以在下面的schemaLocation中无需指定
          xsi:schemaLocation="http://www.springframework.org/schema/beans 
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                          http://www.springframework.org/schema/aop 
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    </beans>

    上面定义了一个名称空间,前缀为xsi,名称空间为http://www.w3.org/2001/XMLSchema-instance。这个名称空间无需在schemaLocation中不存在这个名称空间。

    你可能已经发现了,schemaLocation这个属性其实是w3c定义的属性,与元素一定,属性也需要指定“出处”,xsi:schemaLocation中的xsi就是名称空间前缀。也就是说,上面我们在没有指定xsi名称空间时,就直接使用schemaLocation是错误的。

  • 相关阅读:
    c# 改变FileUpload 上传文件大小
    使用ActiveX读取客户端mac地址
    javascript小技巧
    【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]
    POJ1012 约瑟夫环问题[双向循环链表+打表技巧]
    北大ACM题分类
    ACM大量习题题库
    POJ1423 计算出n的阶乘的位数大数问题[Stirling公式]
    ACM训练计划(下)
    POJ2080 角度问题[cmath函数]
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4184375.html
Copyright © 2020-2023  润新知