A Document Type Definition (DTD) describes the tree structure of a document and something about its data. It is a set of markup affirmations that actually define a type of document for the SGML family, like GML, SGML, HTML, XML.
A DTD can be declared inside an XML document as inline or as an external recommendation. DTD determines how many times a node should appear, and how their child nodes are ordered.
There are 2 data types, PCDATA and CDATA
- PCDATA is parsed character data.
- CDATA is character data, not usually parsed.
Syntax:
<!DOCTYPE element DTD identifier [ first declaration second declaration . . nth declaration ]>
Example:
DTD for the above tree is:
XML document with an internal DTD:
<?xml version="1.0"?>
<!DOCTYPE address [
<!ELEMENT address (name, email, phone, birthday)>
<!ELEMENT name (first, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT birthday (year, month, day)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>
]>
<address>
<name>
<first>Rohit</first>
<last>Sharma</last>
</name>
<email>sharmarohit@gmail.com</email>
<phone>9876543210</phone>
<birthday>
<year>1987</year>
<month>June</month>
<day>23</day>
</birthday>
</address>
The DTD above is interpreted like this:
- !DOCTYPE address defines that the root element of this document is address.
- !ELEMENT address defines that the address element must contain four elements: "name, email, phone, birthday".
- !ELEMENT name defines that the name element must contain two elements: "first, last".
- !ELEMENT first defines the first element to be of type "#PCDATA".
- !ELEMENT last defines the last element to be of type "#PCDATA".
- !ELEMENT email defines the email element to be of type "#PCDATA".
- !ELEMENT phone defines the phone element to be of type "#PCDATA".
- !ELEMENT birthday defines that the birthday element must contain three elements "year, month, day".
- !ELEMENT year defines the year element to be of type "#PCDATA".
- !ELEMENT month defines the month element to be of type "#PCDATA".
- !ELEMENT day defines the day element to be of type "#PCDATA".
XML document with an external DTD:
<?xml version="1.0"?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>
<first>Rohit</first>
<last>Sharma</last>
</name>
<email>sharmarohit@gmail.com</email>
<phone>9876543210</phone>
<birthday>
<year>1987</year>
<month>June</month>
<day>23</day>
</birthday>
</address>
address.dtd:
- <!ELEMENT address (name, email, phone, birthday)>
- <!ELEMENT name (first, last)>
- <!ELEMENT first (#PCDATA)>
- <!ELEMENT last (#PCDATA)>
- <!ELEMENT email (#PCDATA)>
- <!ELEMENT phone (#PCDATA)>
- <!ELEMENT birthday (year, month, day)>
- <!ELEMENT year (#PCDATA)>
- <!ELEMENT month (#PCDATA)>
- <!ELEMENT day (#PCDATA)>
Output: