XML Document Type Definition

Introduction to XML DTD

  • DTD stands for Document Type Definition.
  • It is used to validate the XML documents.
  • XML provides facility to create your own DTDs for XML document.
  • DTD specifies the structure of the XML document.
  • DTD is part of the file or separate document file.

Types of DTD

There are two types of DTDs:
a) Internal DTD.
b) External DTD.

Internal DTD vs External DTD.

Internal DTDExternal DTD
Internal DTD is part of the document.External DTD is a separate file.
The scope is limited to the document which is created.It is accessible across the multiple documents.
Syntax:
<!DOCTYPE rootelement[element and attribute declarations]>
Syntax:
<!DOCTYPE rootelement|SYSTEM “path of .dtd file”>

Components of DTD

We have to associate the .xml file with the DTD and run the same .xml file in the browser.

Syntax for declaring elements in DTD:
<!ELEMENT elementname (content-type or content-model)>

Where elementname specifies the name of the element present in the xml document and content-type or content-model specifies whether the element contains textual data or other elements.

The three types of elements are as follows:

Element typeDescriptionDTD Declaration
EmptyContains attributes, but can't contain text or any other element.<!Element elementname EMPTY>
UnrestrictedContains text content or any other element.<!ELEMENT elementname ANY>
ContainerContain another elements.<!ELEMENT elementname (elementname, elementname [elementname])>

Declaring Attributes in DTD

Syntax:
<!!ATTLIST elementname atributename valuetype [atributetype] [“default”]>

Each attribute declaration must include at least the attribute name in a DTD as follows:

Value TypeDescription
PCDATARepresents the plain text values.
IDAssigns unique value to each element in the document.
(enumerated)Assigns a specific range of values which is specified within parenthesis.

Example : Declaring Attributes in DTD

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
     <!DOCTYPE Employee
[
     <!ELEMENT address (name,company,phone)>
     <!ELEMENT name (#PCDATA)>
     <!ELEMENT company (#PCDATA)>
     <!ELEMENT phone (#PCDATA)>
]>
<address>
     <name>Mayuri S</name>
     <company>TutorialRide</company>
     <phone>91-9800000000</phone>
</address>