VOOZH about

URL: https://www.geeksforgeeks.org/html/document-type-definition-dtd/

⇱ Document Type Definition - DTD - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Document Type Definition - DTD

Last Updated : 6 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:

👁 Image

DTD for the above tree is:

XML document with an internal DTD:


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:

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:

👁 Image
Comment