DTD and XML

Source: Internet
Author: User

 

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:
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Element troops (soldiers +, weapons +)>
  3. <! Element warrior empty>
  4. <! Element weapon (sea, land, empty)>
  5. <! Element sea empty>
  6. <! Element land empty>
  7. <! Element empty>
  8. <! ATTLIST warrior
  9. ID # required
  10. Name CDATA # required
  11. Gender (male | female) # required
  12. Lead idref # required
  13. >
  14. <! ATTLIST sea
  15. Aircraft carrier CDATA # required
  16. Submarine CDATA # required
  17. >
  18. <! ATTLIST Lu
  19. Missile CDATA # required
  20. >
  21. <! ATTLIST null
  22. Fighter CDATA # required
  23. >

 

Write an XML file that references this DTD

Code:
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype Force System "F:/Army. DTD">
  3. <Troops>
  4. <Warrior id = "j001" leader = "j001" Gender = "male" name = "fair"/>
  5. <Warrior id = "j002" leader = "j001" Gender = "female" name = "great"/>
  6. <Weapon>
  7. <Sea aircraft carrier = "Xiaoying" submarine = "advanced"/>
  8. <Land missile = "Mars"/>
  9. <Empty fighter = "J-10"/>
  10. </Weapon>
  11. </Troops>

 

Explanation of the Code:

Code:
  1. <! Element troops (soldiers +, weapons +)>
  2. Element: keyword, used to define elements
  3. Name: element name
  4. Content: Element type
  5. 1) empty this element cannot contain child elements and text, but can have attributes (empty elements)
  6. 2) Any. This element can contain any element defined in the DTD.
  7. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.