Chapter 4. Document type definitions
Defining XML vocabularies with DTDs
Creating your own XML mark up is all well and good, but if you want to share your documents with other people you will need to communicate to these other people the vocabulary your XML documents understand. This is the semantic part of XML documents -- what elements do your XML files contain and how are the elements related to each other? These semantic relationships are created using Docu- ment Type Definitions (DTD) and/or XML Schemas. DTDs are legacy implementations from the SGML world. They are more commonly used than the newer, XML-based, XML Schemas. This section provides an overview for creating DTDs.
DTDs can exist inside an XML document or outside an XML document. If they reside in an XML docu- ment, then they begin with a DOCTYPE declaration followed by the name of the XML document's root element and finally a list of all the elements and how they are related to each other. Here is a simple DTD for embedded in the pets.xml file itself:
<!DOCTYPE pets [
(pet+)> (name, age, type, color)> (#PCDATA)> (#PCDATA)> (#PCDATA)> r (#PCDATA)>
<pets> <pet> <name>Tilly</name> <age>14</age> <type>cat</type> <color>silver</color> </pet> <pet> <name>Amanda</name> <age>10</age> <type>dog</type> <color>brown</color> </pet> <pet>