DTD
DTD is an effective method to ensure that the XML document format is correct. You can compare the XML document and DTD file to see whether the document complies with the specifications, and whether the elements and labels are correctly used.
Example: Army. DTD
Code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Element troops (soldiers +, weapons +)>
- <! Element warrior empty>
- <! Element weapon (sea, land, empty)>
- <! Element sea empty>
- <! Element land empty>
- <! Element empty>
- <! ATTLIST warrior
- ID # required
- Name CDATA # required
- Gender (male | female) # required
- Lead idref # required
- >
- <! ATTLIST sea
- Aircraft carrier CDATA # required
- Submarine CDATA # required
- >
- <! ATTLIST Lu
- Missile CDATA # required
- >
- <! ATTLIST null
- Fighter CDATA # required
- >
Write an XML file that references this DTD
Code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype Force System "F:/Army. DTD">
- <Troops>
- <Warrior id = "j001" leader = "j001" Gender = "male" name = "fair"/>
- <Warrior id = "j002" leader = "j001" Gender = "female" name = "great"/>
- <Weapon>
- <Sea aircraft carrier = "Xiaoying" submarine = "advanced"/>
- <Land missile = "Mars"/>
- <Empty fighter = "J-10"/>
- </Weapon>
- </Troops>
Explanation of the Code:
Code:
- <! Element troops (soldiers +, weapons +)>
- Element: keyword, used to define elements
- Name: element name
- Content: Element type
- 1) empty this element cannot contain child elements and text, but can have attributes (empty elements)
- 2) Any. This element can contain any element defined in the DTD.
- 3) # pcdata can contain any character data, but cannot contain any child element
Modifier
Symbol |
Purpose |
Example |
Description |
() |
Used to Group Elements |
(Gu Long | Jin Yong) |
|
| |
Select |
(Man | woman) |
|
+ |
This object appears at least once and can appear multiple times |
(Member +) |
|
* |
(0 to multiple times) |
(Hobby *) |
|
? |
This object can appear only once (0 to 1) |
(Cainiao ?) |
|
, |
The object must appear in the specified order |
(Watermelon, apple, banana) |
|
Attribute definition Syntax: <! ATTLIST element name attribute name type attribute features> type cdata id idref idrefs enumerated attribute features: # requed # implied # fixed