Some Simple DTD Examples
Let's take a quick look at two DTDsone internal and one external. Listing 3.1 shows an internal DTD.
Listing 3.1 An Internal DTD
<?xml version="1.0"?> <!DOCTYPE message [ <!ELEMENT message (#PCDATA)> ]> <message> Let the good times roll! </message>
In Listing 3.1, the internal DTD is contained within the Document Type Declaration, which begins with <!DOCTYPE and ends with ]>. The Document Type Declaration will appear between the XML declaration and the start of the document itself (the document or root element) and identify that section of the XML document as containing a Document Type Definition. Following the Document Type Declaration (DOCTYPE), the root element of the XML document is defined (in this case, message). The DTD tells us that this document will have a single element, message, that will contain parsed character data (#PCDATA).
The Document Type Declaration should not be confused with the Document Type Definition. These are two exclusive items. Also confusing is the acronym DTD, which is only ever used in reference to the Document Type Definition. The Document Type Declaration is the area of the XML document after the XML declaration that begins with <!DOCTYPE and ends with ]>. It actually encompasses the Document Type Definition. The Document Type Definition will be contained within an opening bracket ([) and a closing bracket (]).
Now, let's take a look at Listing 3.2 and see how this same DTD and XML document would be joined if the DTD were external.
Listing 3.2 An External DTD
<?xml version="1.0"?> <!DOCTYPE message SYSTEM "message.dtd"> <message> Let the good times roll! </message>
In Listing 3.2 the DTD is contained in a separate file, message.dtd. The contents of message.dtd are assumed to be the same as the contents of the DTD in Listing 3.1. The keyword SYSTEM in the Document Type Declaration lets us know that the DTD is going to be found in a separate file. A URL could have been used to define the location of the DTD. For example, rather than message.dtd, the Document Type Declaration could have specified something like ../DTD/message.dtd.
The keyword SYSTEM used in a Document Type Declaration will always be indicative of the Document Type Definition being contained in an external file.
Both of these examples show us a well-formed XML document. Additionally, because both XML documents contain a single element, message, which contains only parsed character data, both adhere to the DTD. Therefore, they are both also valid XML documents.
A document that looks like what's shown in Listing 3.3 would not be valid according to the DTD in these examples.
Listing 3.3 Document Not Valid According to Defined DTD
<?xml version="1.0"?> <!DOCTYPE message SYSTEM "message.dtd"> <message> <text> Let the good times roll! </text> </message>
Even though this is a well-formed XML document, it is not valid. When this document is validated against message.dtd, a flag will be raised because message.dtd does not define an element named text.
Don't worry if you do not completely understand what is going on at this point. As long as you get the gist, everything will become very clear in the sections that follow.