Document Type Definition - DTD - GeeksforGeeks

06 Nov, 2020
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
<?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 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”.
!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
<?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 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)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
HTML and XML
Web technologies-HTML and XML
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript
Top 10 Angular Libraries For Web Developers
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to redirect to another page in ReactJS ?
How to Insert Form Data into Database using PHP ?
How to execute PHP code using command line ?
Set the value of an input field in JavaScript