I. Definition
The document type defines a DTD, which is specifically used to detect whether the XML conforms to the syntax rules.
Two. Example
Two ways to declare a document type:
1. Internal definition
Persons.xml file
<?xml version= "1.0" encoding= "UTF-8"?><!--<! DOCTYPE root element [element declaration]>--><! DOCTYPE persons[<!--commonly used control characters:? : 0-1 times; *:0-n times; +:1-n times; Default: 1 times--><! ELEMENT persons (person+) ><! ELEMENT person (name, password, age) ><!--#PCDATA is a term used by the XML parser to parse the text data--><!--CDATA appears in the property declaration--><! ELEMENT name (#PCDATA) ><! ELEMENT password (#PCDATA) ><! ELEMENT Age (#PCDATA) ><!--#REQUIRED属性值是必需的--><!--#IMPLIED属性不是必需的--><!--The Value property values are fixed-- ><!--ID value is a unique ID--><! ATTLIST Person ID ID #REQUIRED ><!--CDATA value is character data (character)--><! attlist person Email CDATA #REQUIRED >]><persons><person id= "p1070541038" email= "[email protected ] "><name> Zhaoli </name><password>000000</password><age>26</age><!--< id>p1070541038</id>--><!--Error--><!--<email>[email protected]</email>-- ></person><person id= "p20705410"Email=" [email protected] "><name> Zhaoli </name><password>000000</password>< Age>26</age></person></persons>
2. External definition
CLASSROOM.DTD file
<?xml version= "1.0" encoding= "UTF-8"? ><! ELEMENT Classroom (name,grade,students) ><! Attlist Classroom ID #REQUIRED ><! ELEMENT name (#PCDATA) ><! ELEMENT Grade (#PCDATA) ><! ELEMENT students (student+) ><! ELEMENT Student (Id,name,age) ><! ELEMENT ID (#PCDATA) ><! ELEMENT name (#PCDATA) ><! ELEMENT Age (#PCDATA) >
Classroom.xml file
<?xml version= "1.0" encoding= "UTF-8"?><!--External Document declaration--><!--if the DTD is outside the XML source file, it should be encapsulated in a DOCTYPE by the following syntax Righteousness in:--><! DOCTYPE classroom SYSTEM "CLASSROOM.DTD" ><classroom id= "Classroomid1" ><name>classname1</ name><grade>grade1</grade><students><student><id>studentid1</id>< Name>studentname1</name><age>studentage1</age></student><student><id> Studentid2</id><name>studentname2</name><age>studentage2</age></student> </students></classroom>
three. Reviews1. Advantages:
Simple and straightforward, easy to write.
To detect if XML is valid
2. Disadvantages:
1) different from XML syntax
2) Provide a limited number of data types that users cannot customize
3) does not support the domain name mechanism
"WebService" (ii) the DTD of XML