Before you begin building DTD documents, you should understand that a DTD can be defined in several places. A DTD is a set of text elements that follows a specific syntax. This text can be stored in the XML file that it defines, or it can be held in a separate file. If it is held within the same file as the XML it defines, then it is considered an inline DTD. If it is kept in a separate file, it is considered an external DTD and has a .dtd file extension.
To show an example of utilizing an internal DTD, suppose you are working with the XML document presented in Listing 1
Listing 1: An XML document that needs a DTD
<?xml version="1.0" encoding="UTF-8" ?>
<Process>
<Name>Bill Evjen</Name>
<Address>123 Main Street</Address>
<City>Saint Charles</City>
<State>Missouri</State>
<Country>USA</Country>
<Order>
<Item>52-inch Plasma</Item>
<Quantity>1</Quantity>
</Order>
</Process>
To place a DTD document within this XML document, you place the DTD definition directly after the <?xml> declaration as illustrated in Listing 2.
Listing 2: Providing the XML document an internal DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Process [
<!ELEMENT Address (#PCDATA)>
<!ELEMENT City (#PCDATA)>
<!ELEMENT Country (#PCDATA)>
<!ELEMENT Item (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Order (Item, Quantity)>
<!ELEMENT Process (Name, Address, City, State, Country, Order)>
<!ELEMENT Quantity (#PCDATA)>
<!ELEMENT State (#PCDATA)>
]>
<Process>
<Name>Bill Evjen</Name>
<Address>123 Main Street</Address>
<City>Saint Charles</City>
<State>Missouri</State>
<Country>USA</Country>
<Order>
<Item>52-inch Plasma</Item>
<Quantity>1</Quantity>
</Order>
</Process>
At this point, I won't review the meaning of the DTD declaration. Note that it is possible to declare DTDs internally within the XML document. Many in the industry, however, would say this is an improper way to declare your DTDs. They would tell you it is better to provide some level of abstraction and declare your DTDs externally. Others prefer that the DTD that validates the XML document is encased within the document itself-eliminating the need to deal with two files.