XML, as a markup language, is defined by a document (Document Type Definition DTD). DTD can be seen as a template for a class of XML documents. It defines the logical structure of the document and specifies the elements, entities, attributes, and relationships between elements and entities used in the XML document. It enables data exchange and sharing, and verifies the effectiveness of data. DTD can be a completely independent file, or can be directly set in an XML file. Therefore, DTD can be divided into two types: External DTD (calling another edited DTD in the XML file) and internal DTD (directly setting DTD in the XML file. In some companies with business dealings, if the electronic documents they use are XML documents, we can use an independent DTD document. It is referenced in each exchange and definition to verify the validity of the structure integrity and syntax. For example, use the following XML document:
<? XML version = "1.0" encoding = "gb2312" standalone = "yes" ?>
< Student list >
< Student >
< Student ID > 2003081205 </ Student ID >
< Name > Tian Lin </ Name >
< Class > 03.2 class </ Class >
</ Student >
< Student >
< Student ID > 2003081232 </ Student ID >
< Name > Yang Xuefeng </ Name >
< Class > 03.2 class </ Class >
</ Student >
</ Student list >
One of its DTD documents is as follows:
1 <? XML version = "1.0" encoding = "gb2312" standalone = "yes" ?>
2 < ! Doctype Student list [
3 <! Element student ID (# pcdata) >
4 <! Element name (# pcdata) >
5 <! Element class (# pcdata) >
6 <! Element student (student ID, name, class) >
7 <! Element student list (student, student) >
8 ]>
9
10
11
2. Define the start tag for the DTD. The student list is its root element. From 3 to 7, the start tag is the element definition, and from 8 to the end tag. The following describes the syntax and precautions of element declaration.
Element Declaration
Format: <! Element element name definition of the content format of an element>
(1) Basic Element Declaration: <! Element student ID (# pcdata)> after the element name is followed directly by the data type, it is the basic element.
(2) composite element declaration:<!Element student (student ID, name, class)> The Student element is a composite element. It contains three basic elements: Student ID, name, and class. In fact, the root element is a special case of composite elements. All elements are directly or indirectly contained in the root element.
Control the number of element occurrences
Add "? "Indicates that some elements can appear 0 or 1 time." * "indicates that they can appear any time." + "indicates that they must appear at least once. For example:<!Element student (student ID ?, Name, class, hobbies *)> It indicates that a student has only a unique student ID, or has not been assigned a student ID. That is, the student ID element appears 0 or once, And the hobby can be absent or has several aspects, therefore, use * (any time.
The optional elements are limited by "|", as shown in figure <! Element student (name, gender, (excellent | good | moderate)>
attribute declaration
Format:
! ATTLIST is an instruction for defining attributes. It is followed by the element name, attribute name, attribute value type, keyword of the default value, and default value.
if a student has two attributes: gender and class, you can define them as follows:
attribute that must be assigned: add the keyword required, for example: indicates that the Gender value of the student must be given. Optional properties use the implied keyword, and fixed values use the fixed keyword.
attribute type: there are 10 attribute types in XML, table:
Type |
Description |
CDATA |
Character data |
Enumerated |
List of possible values |
ID |
Unique number |
Idref |
ID type Attribute Value |
Idrefs |
IDS separated by Spaces |
Entity |
Entity |
Entitys |
Several entities |
Nmtoken |
XML name |
Notation |
Annotation name declared in DTD |
Nmtokens |
Multiple XML names |
Previous Article: XML syntax Overview