XML 约束

一、约束

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

  要求

    1、能够在 xml 中引入约束文档

    2、能够简单的读懂约束文档

  分类

    1、DTD:一种简单的约束技术(后缀.dtd)

    2、Schema:一种复杂的约束技术(后缀.xsd)

二、DTD 约束

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

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

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

外部本地文件:<!DOCTYPE 根标签名 SYSTEM "dtd文件的位置">

外部网络文件:<!DOCTYPE 根标签名 PUBLIC "dtd文件名字" "dtd文件的位置URL">

  

  2、案例

    声明 student.dtd 约束

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

      数量词:

? 表示元素可以出现0次到1次

+ 表示元素至少出现一次

*  表示元素可以出现0到多次

  

    student.xml 文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE students SYSTEM "student.dtd">
 3 
 4 <!--<!DOCTYPE students [   // 可以在内部这样引入约束
 5 
 6         <!ELEMENT students (student+) >
 7         <!ELEMENT student (name,age,sex)>
 8         <!ELEMENT name (#PCDATA)>
 9         <!ELEMENT age (#PCDATA)>
10         <!ELEMENT sex (#PCDATA)>
11         <!ATTLIST student number ID #REQUIRED>
12  
13 
14         ]>-->
15 <students>
16     
17     <student number="s001">
18         <name>zhangsan</name>
19         <age>abc</age>
20         <sex>hehe</sex>
21     </student>
22 
23     <student number="s002">
24         <name>lisi</name>
25         <age>24</age>
26         <sex>female</sex>
27     </student>
28     
29 </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"

    (4)为每一个xsd约束声明一个前缀,作为标识

xmlns="http://www.itcast.cn/xml" 

  2、案例

    声明 student.xsd 约束文件

 1 <?xml version="1.0"?>
 2 <xsd:schema xmlns="http://www.itcast.cn/xml"
 3         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 4         targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
 5     <!-- 里面有一个根元素students,类型为自定义-->
 6     <xsd:element name="students" type="studentsType"/>
 7     <!-- 自定义的 students 类型复杂类型-->
 8     <xsd:complexType name="studentsType">
 9         <!-- 属性是有序的 -->
10         <xsd:sequence>
11             <!-- 子标签为 student,类型为自定义,最少一个,最多无限-->
12             <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
13         </xsd:sequence>
14     </xsd:complexType>
15     <!-- 自定义student类型 复杂类型-->
16     <xsd:complexType name="studentType">
17         <!-- 各个属性都是有序的 -->
18         <xsd:sequence>
19             <xsd:element name="name" type="xsd:string"/>
20             <xsd:element name="age" type="ageType" />
21             <xsd:element name="sex" type="sexType" />
22         </xsd:sequence>
23         <!-- student 标签有一个number属性,自定义类型,必须有 -->
24         <xsd:attribute name="number" type="numberType" use="required"/>
25     </xsd:complexType>
26     <!-- 自定义简单类型 -->
27     <xsd:simpleType name="sexType">
28         <!-- 里面的值为字符串,切取值有限制-->
29         <xsd:restriction base="xsd:string">
30             <xsd:enumeration value="male"/>
31             <xsd:enumeration value="female"/>
32         </xsd:restriction>
33     </xsd:simpleType>
34     <!-- 自定义简单类型-->
35     <xsd:simpleType name="ageType">
36         <!-- 里面的值为整形,取值范围 0-256 -->
37         <xsd:restriction base="xsd:integer">
38             <xsd:minInclusive value="0"/>
39             <xsd:maxInclusive value="256"/>
40         </xsd:restriction>
41     </xsd:simpleType>
42     <!-- 自定义简单类型-->
43     <xsd:simpleType name="numberType">
44         <!-- 里面的值为字符串,切以heima开头,后面跟4位数字-->
45         <xsd:restriction base="xsd:string">
46             <xsd:pattern value="heima_d{4}"/>
47         </xsd:restriction>
48     </xsd:simpleType>
49 </xsd:schema> 

    student.xml 文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!-- 
 3     1.填写xml文档的根元素
 4     2.引入xsi前缀.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     3.引入xsd文件命名空间.  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
 6     4.为每一个xsd约束声明一个前缀,作为标识  xmlns="http://www.itcast.cn/xml" 
 7     
 8     
 9  -->
10 <students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11             xmlns="http://www.itcast.cn/xml"
12             xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
13 
14 >
15     <student number="heima_0001">
16         <name>tom</name>
17         <age>18</age>
18         <sex>male</sex>
19     </student>
20 
21 </students>

  3、

原文地址:https://www.cnblogs.com/niujifei/p/15093086.html