XML document types

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:

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.

A DTD is not required for an XML document. However, a valid XML document has a DTD and conforms to that DTD.