DTD Introduction
- Previous Page DTD Tutorial
- Next Page DTD Building Module
Document Type Definition (DTD) can define the building blocks of a valid XML document. It uses a set of valid elements to define the structure of the document.
DTD can be declared line by line in the 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:
<!DOCTYPE root element [element declarations]>
An XML document instance with DTD (please open in IE5 or higher versions and select View Source):
<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
Open this XML file in your browser and select the "View Source" command.
The above DTD explanation is as follows:
!DOCTYPE note (Second line) Define this document as note type of document.
!ELEMENT note (Third line) Definition note The element has four elements: "to, from, heading, body"
!ELEMENT to (Fourth line) Definition to Element of type "#PCDATA"
!ELEMENT from (Fifth line) Definition from Element of type "#PCDATA"
!ELEMENT heading (Sixth line) Definition heading Element of type "#PCDATA"
!ELEMENT body (Seventh line) Definition body Element of type "#PCDATA"
External document declaration
If the DTD is located outside the XML source file, it should be enclosed in a DOCTYPE definition with the following syntax:
<!DOCTYPE root-element SYSTEM "filename">
This XML document is the same as the above XML document, but it has an external DTD: (Open in IE5,and then select the "View Source" command.)
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
This is the "note.dtd" file containing DTD:
<!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
Why use DTD?
Through DTD, each XML file can carry a description of its own format.
Through DTD, independent groups can use a standard DTD to exchange data consistently.
And your application can also use a standard DTD to validate data received from external sources.
You can also use DTD to validate your own data.
- Previous Page DTD Tutorial
- Next Page DTD Building Module