Posted on 18th February 2014

You can define certain rules that an XML document should conform for it to be valid. These rules are called Document Type Definitions or DTDs. DTDs defines what elements and attributes are used to form the document and how they are used.

Document type declaration is the place where you declare the DTDs. These declarations are placed in the XML prolog, just after the XML declaration. Document type declaration can be placed inside the document itself, in an external file or both. The internal declarations are called the internal subset and the external declarations are called the external subset.

External declarations are NOT parsed if standalone=yes in the XML declaration.

A document type declaration has the general form

<!DOCTYPE root_element_name DTD-Identifier [
] >

DOCTYPE : Document type declaration starts with the delimiter <!DOCTYPE and ends with the delimiter >

root_element_name : The <!DOCTYPE delimiter is followed by name of the first element in the document also called the root element.The root element name can be any valid XML element name.

DTD-Identifier : A DTD-Identifier is used to reference external declarations. The external declarations file is located using a System identifier or a Public identifier. System identifier use the keyword SYSTEM followed by an URI that point to an external DTD file.


Here is an example of system identifier

<!DOCTYPE catalog SYSTEM "http://www.opentechguides.com/catalog.dtd">

Public identifier use the keyword PUBLIC followed by a Formal Public Identifier (FPI) and optionally a URI.


An FPI is a globally unique name that identifies the document and is of the format

prefix//owner-identifier//text-class text-description//language//display version

prefix is either a + (for registered identifier) or a - (for unregistered identifier).

owner-identifier is a unique string that identifies the owner or it can be a domain name or company name.

text-class identifies the document type.

language is the language in which the document is written.

display-version is an optional text that specifies the text display version.

Here is an example of a public identifier

<!DOCTYPE catalog PUBLIC "-//W3C//DTD CATALOG//EN" "http://www.opentechguides.com/catalog.dtd">

Declarations :The declarations area contains Element declarations, Attribute declarations, Entity declarations and Notation Declarations. These declaration types are explained in detail in the later chapters.

Internal Declaration Example

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!DOCTYPE catalog [
 <!ELEMENT catalog (movie+)>
 <!ELEMENT movie (title,year,genre,director,actor+)>
 <!ELEMENT title (#PCDATA)>
 <!ELEMENT year (#PCDATA)>
 <!ELEMENT genre (#PCDATA)>
 <!ELEMENT director (#PCDATA)>
 <!ELEMENT actor (#PCDATA)>

External Declaration Example

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd" >

Where catalog.dtd file is in the same location as the XML document and contains the following declarations

 <!ELEMENT catalog (movie+)>
 <!ELEMENT movie (title,year,genre,director,actor+)>
 <!ELEMENT title (#PCDATA)>
 <!ELEMENT year (#PCDATA)>
 <!ELEMENT genre (#PCDATA)>
 <!ELEMENT director (#PCDATA)>
 <!ELEMENT actor (#PCDATA)>

Post a comment


Nothing yet..be the first to share wisdom.