A Document Type Definition (DTD) defines the structure of a class of XML documents, making it possible to distinguish between classes. A DTD is a list of element and attribute definitions unique to a class. Once you have set up a DTD, you can reference a DTD in another document or embed the DTD in the XML document.
Here is another example of an XML document:
<?xml version="1.0"?> <Info> <OneTag>1999/07/04</OneTag> <AnotherTag>123</AnotherTag> <LastTag>Acme Alpha</LastTag>
<Thing> <ThingId>987</ThingId> <ThingName>Coupler</ThingName> <Amount>5</Amount> </Thing>
<Thing> <ThingId>654</ThingId> <ThingName>Connecter</ThingName> </Thing>
<Thing> <ThingId>579</ThingId> <ThingName>Clasp</ThingName> <Amount>1</Amount> </Thing> </Info>
This example, called Info, is a well-formed document and has the same structure and data as the XML Order document. However, Info is not recognized by a processor designed for Order documents, because each has a different DTD.
The DTD for XML Order documents is:
<!ELEMENT Order (Date, CustomerId, CustomerName, Item+)> <!ELEMENT Date (#PCDATA)> <!ELEMENT CustomerId (#PCDATA)> <!ELEMENT CustomerName (#PCDATA)> <!ELEMENT Item (ItemId, ItemName, Quantity)> <!ELEMENT ItemId (#PCDATA)> <!ELEMENT ItemName (#PCDATA)> <!ELEMENT Quantity (#PCDATA)> <!ATTLIST Quantity units CDATA #IMPLIED>
This DTD specifies that:
An order consists of these required elements: a date, a customer ID, a customer name, and one or more items indicated by “+”. These items are required. A question mark indicates an optional element, for example, “CustomerName?”. An asterisk indicates that an element can occur zero or more times (for example, “Item*”).
Elements defined by “(#PCDATA)” are character text.
The “<ATTLIST…>” definition specifies that quantity elements have a “units” attribute; the “#IMPLIED” specification indicates that the “units” attribute is optional.
The character text of XML documents is not constrained. For example, there is no way to specify that the text of a quantity element should be numeric, so the following is valid:
<Quantity unit=”Baker’s dozen”>three</Quantity>
<Quantity unit=”six packs”>plenty</Quantity>
Restrictions on the text of elements are handled by applications that process XML data.
A DTD follows the <?xml version="1.0"?>
instruction
in an XML document. You can either include the DTD within your XML
document, or you can reference an external DTD.
This example references an external DTD:
<?xml version="1.0"?> <!DOCTYPE Order SYSTEM "Order.dtd”> <Order> … </Order>
This example contains an embedded DTD:
<?xml version="1.0"?> <!DOCTYPE Order [ <!ELEMENT Order (Date, CustomerId, CustomerName, Item+)> <!ELEMENT Date (#PCDATA) <!ELEMENT CustomerId (#PCDATA)> <!ELEMENT CustomerName (#PCDATA)> <!ELEMENT Item (ItemId, ItemName, Quantity)> <!ELEMENT ItemId (#PCDATA)> <!ELEMENT ItemName (#PCDATA)> <!ELEMENT Quantity (#PCDATA)> <!ATTLIST Quantity units CDATA #IMPLIED>
]> <Order> <Date>1999/07/04</Date> <CustomerId>123</CustomerId> <CustomerName>Acme Alpha</CustomerName>
<Item> … </Item> </Order>
A DTD is not required for an XML document. However, a valid XML document has a DTD and conforms to that DTD.