DTD Introduction
Document Type Definition (DTD) defines the legal building blocks of an XML document. It uses a set of legal elements to define the structure of the document.
DTD can be declared inline in the XML document or referenced as an external file.
Internal DOCTYPE Declaration
If the DTD is included within your XML source file, it should be wrapped in a DOCTYPE declaration using the following syntax:
XML document instance with DTD (open in IE5 or higher and select "View Source"):
Open this XML file in your browser and select the "View Source" command.
The above DTD is explained as follows:
- !DOCTYPE note (second line) defines that this document is a note type document.
- !ELEMENT note (third line) defines that the note element has four elements: "to, from, heading, body"
- !ELEMENT to (fourth line) defines the to element as "#PCDATA" type
- !ELEMENT from (fifth line) defines the from element as "#PCDATA" type
- !ELEMENT heading (sixth line) defines the heading element as "#PCDATA" type
- !ELEMENT body (seventh line) defines the body element as "#PCDATA" type
External Document Declaration
If the DTD is located outside the XML source file, it should be wrapped in a DOCTYPE definition using the following syntax:
This XML document is the same as the one above but has an external DTD: (Click to open the file and select "View Source" command.)
This is the "note.dtd" file containing the DTD:
Why Use DTD?
With DTD, every XML file can carry a description of its own format.
With 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 DTD to validate your own data.