天天看点

11Java基础加强-4. xml文档约束(dtd,schema)

约束

约束:规定 xml 文档的书写规则

要求:

  1. 能够在 xml 中引入约束文档
  2. 能够简单的读懂约束文档

分类:

  1. DTD:一种简单的约束技术(后缀.dtd)
  2. Schema:一种复杂的约束技术(后缀.xsd)

DTD 约束

  1. 引入 dtd 文档到 xml 文档中

    内部 dtd:将约束规则定义在 xml 文档中

    外部 dtd:将约束规则定义在外部的 dtd 文件中

    外部本地文件:

    外部网络文件:

  2. 案例

    student.dtd 约束

<!-- 一个根元素,叫students,里面可以有0-n个student-->
<!ELEMENT students (student*) >
<!-- 一个student 里面有 name,age,sex,三个标签,且书写按照顺序-->
<!ELEMENT student (name,age,sex)>
<!-- name 里面存放数据-->
<!ELEMENT name (#PCDATA)>
<!-- age 里面存放数据-->
<!ELEMENT age (#PCDATA)>
<!-- sex 里面存放数据-->
<!ELEMENT sex (#PCDATA)>
<!-- student 标签有一个number属性,是作为id属性,而且是必须有的-->
<!ATTLIST student number ID #REQUIRED>      

student.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">

<!--<!DOCTYPE students [   // 也可以这样引入约束

        <!ELEMENT students (student+) >
        <!ELEMENT student (name,age,sex)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT age (#PCDATA)>
        <!ELEMENT sex (#PCDATA)>
        <!ATTLIST student number ID #REQUIRED>


        ]>-->
<students>

    <student number="s001">
        <name>zhangsan</name>
        <age>abc</age>
        <sex>hehe</sex>
    </student>

    <student number="s002">
        <name>lisi</name>
        <age>24</age>
        <sex>female</sex>
    </student>

</students>      

Schema

  1. 引入约束文件

    (1)填写 xml 文档的根元素

    (2)引入 xsi 前缀

    xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

(3)引入 xsd 文件命名空间

xsi:schemaLocation=“http://www.itcast.cn/xml student.xsd”

  1. 案例

    student.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">
    <!-- 里面有一个根元素students,类型为自定义-->
    <xsd:element name="students" type="studentsType"/>
    <!-- 自定义的 students 类型复杂类型-->
    <xsd:complexType name="studentsType">
        <!-- 属性是有序的 -->
        <xsd:sequence>
            <!-- 子标签为 student,类型为自定义,最少一个,最多无限-->
            <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <!-- 自定义student类型 复杂类型-->
    <xsd:complexType name="studentType">
        <!-- 各个属性都是有序的 -->
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="age" type="ageType" />
            <xsd:element name="sex" type="sexType" />
        </xsd:sequence>
        <!-- student 标签有一个number属性,自定义类型,必须有 -->
        <xsd:attribute name="number" type="numberType" use="required"/>
    </xsd:complexType>
    <!-- 自定义简单类型 -->
    <xsd:simpleType name="sexType">
        <!-- 里面的值为字符串,切取值有限制-->
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="male"/>
            <xsd:enumeration value="female"/>
        </xsd:restriction>
    </xsd:simpleType>
    <!-- 自定义简单类型-->
    <xsd:simpleType name="ageType">
        <!-- 里面的值为整形,取值范围 0-256 -->
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="256"/>
        </xsd:restriction>
    </xsd:simpleType>
    <!-- 自定义简单类型-->
    <xsd:simpleType name="numberType">
        <!-- 里面的值为字符串,切以heima开头,后面跟4位数字-->
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="heima_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>      
<?xml version="1.0" encoding="UTF-8" ?>
<!--
    1.填写xml文档的根元素
    2.引入xsi前缀.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.引入xsd文件命名空间.  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
    4.为每一个xsd约束声明一个前缀,作为标识  xmlns="http://www.itcast.cn/xml"


 -->
<students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://www.itcast.cn/xml"
            xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"

>
    <student number="heima_0001">
        <name>tom</name>
        <age>18</age>
        <sex>male</sex>
    </student>

</students>