DTD Introduction
A document type definition (DTD) defines the legal building blocks of an XML document. It uses a series of legal elements to define the structure of the document.
A DTD can be declared inline in an XML document or as an external reference.
Internal DOCTYPE declaration
If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE declaration with the following syntax:
Example of an XML document with a DTD (open it in IE5 or later and choose View Source):
]>
Open this XML file in your browser and choose the "View Source" command.
The DTD above is explained as follows:
- !DOCTYPE note (Line 2) defines this document as a note type document.
- !ELEMENT note (Line 3) defines that the note element has four elements: "to, from, heading, body"
- !ELEMENT to (Line 4) defines the to element as the "#PCDATA" type
- !ELEMENT from (Line 5) defines from element as the "#PCDATA" type
- !ELEMENT heading (Line 6) defines heading element as the "#PCDATA" type
- !ELEMENT body (Line 7) defines body element as the "#PCDATA" type
External document declaration
If the DTD is outside the XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:
This XML document is the same as the one above, but has an external DTD: (click to open this file, and choose the "View Source" command.)
Don't forget me this weekend!
This is the "note.dtd" file containing the DTD:
Why use a DTD?
With a DTD, each of your XML files can carry a description of its own format.
With a DTD, independent groups can consistently use a standard DTD to exchange data.
Your application can also use a standard DTD to validate data received from outside.
You can also use a DTD to validate your own data.