JavaWeb12.2【XML:约束、DTD、Schema(XSD)】
DTD --> XML
1 <!ELEMENT students (student*) > <!--*表示可以出现无限次--> 2 <!ELEMENT student (name,age,sex)> <!--严格属性顺序:name,age,sex--> 3 <!ELEMENT name (#PCDATA)> <!--PCDATA表示字符串类型数据--> 4 <!ELEMENT age (#PCDATA)> 5 <!ELEMENT sex (#PCDATA)> 6 <!ATTLIST student number ID #REQUIRED> <!--student的属性number是ID,必选--> 7 8 <!--这是一个外部的dtd约束文件,即规定xml文档的书写规则-->
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!--引入本地外部dtd约束文件的声明--> 3 <!DOCTYPE students SYSTEM "student.dtd"> 4 5 <!--内部dtd约束定义方式--> 6 <!--<!DOCTYPE students[ 7 <!ELEMENT students (student*) > 8 <!ELEMENT student (name,age,sex)> 9 <!ELEMENT name (#PCDATA)> 10 <!ELEMENT age (#PCDATA)> 11 <!ELEMENT sex (#PCDATA)> 12 <!ATTLIST student number ID #REQUIRED> 13 ]>--> 14 15 <students> 16 <student number="itcast_0001"> 17 <name>tom</name> 18 <age>9999</age> 19 <sex>male</sex> 20 </student> 21 <student number="itcast_0002"> 22 <name>sam</name> 23 <age>20</age> 24 <sex>female</sex> 25 </student> 26 <!--<aaa></aaa> 不按约束写标签会变红报错--> 27 </students>
XSD --> XML
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 6 <xsd:element name="students" type="studentsType"/> 7 <xsd:complexType name="studentsType"> 8 <xsd:sequence> 9 <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/> 10 </xsd:sequence> 11 </xsd:complexType> 12 <xsd:complexType name="studentType"> 13 <xsd:sequence> 14 <xsd:element name="name" type="xsd:string"/> 15 <xsd:element name="age" type="ageType" /> 16 <xsd:element name="sex" type="sexType" /> 17 </xsd:sequence> 18 <xsd:attribute name="number" type="numberType" use="required"/> 19 </xsd:complexType> 20 <xsd:simpleType name="sexType"> 21 <xsd:restriction base="xsd:string"> 22 <xsd:enumeration value="male"/> 23 <xsd:enumeration value="female"/> 24 </xsd:restriction> 25 </xsd:simpleType> 26 <xsd:simpleType name="ageType"> 27 <xsd:restriction base="xsd:integer"> 28 <xsd:minInclusive value="0"/> 29 <xsd:maxInclusive value="256"/> 30 </xsd:restriction> 31 </xsd:simpleType> 32 <xsd:simpleType name="numberType"> 33 <xsd:restriction base="xsd:string"> 34 <xsd:pattern value="heima_\d{4}"/> 35 </xsd:restriction> 36 </xsd:simpleType> 37 38 </xsd:schema>
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 <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 9 xsi:schemaLocation="http://www.itcast.cn/xml student.xsd" 10 xmlns="http://www.itcast.cn/xml"> <!--使用了默认的空前缀--> 11 12 <student number="heima_0001"> 13 <name>tom</name> 14 <age>18</age> 15 <sex>male</sex> 16 </student> 17 <!--<student number="hehe_0001"> 报错 18 <name>tom</name> 19 <age>999</age> 报错 20 <sex>男</sex> 报错 21 </student>--> 22 23 </students> 24 25 26 <!-- 27 <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 28 xsi:schemaLocation="http://www.itcast.cn/xml student.xsd 29 http://www.itcast.cn/xml2 student2.xsd 30 http://www.itcast.cn/xml3 student3.xsd" 31 xmlns="http://www.itcast.cn/xml" 32 xmlns:b="http://www.itcast.cn/xml2"> 33 xmlns:c="http://www.itcast.cn/xml3">--> 34 <!--前缀用于区分不同xsd约束中的同名内容-->