限制
限制:規定 xml 文檔的書寫規則
要求:
- 能夠在 xml 中引入限制文檔
- 能夠簡單的讀懂限制文檔
分類:
- DTD:一種簡單的限制技術(字尾.dtd)
- Schema:一種複雜的限制技術(字尾.xsd)
DTD 限制
-
引入 dtd 文檔到 xml 文檔中
内部 dtd:将限制規則定義在 xml 文檔中
外部 dtd:将限制規則定義在外部的 dtd 檔案中
外部本地檔案:
外部網絡檔案:
-
案例
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)填寫 xml 文檔的根元素
(2)引入 xsi 字首
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
(3)引入 xsd 檔案命名空間
xsi:schemaLocation=“http://www.itcast.cn/xml student.xsd”
-
案例
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>