XML Menu


Document Type Declaration(DTD)




XML Document Type Declaration(DTD) is a way to describe precisely the XML language. DTD is an XML technique used to define the structure of a XML document. It's useful to make sure the structure and vocabulary of XML documents are valid against the grammatical rules of the appropriate XML language.

A DTD can contain declarations that define elements, attributes, notations, and entities for any XML files that reference the DTD file. It also establishes constraints for how each element, attribute, notation, and entity can be used within any of the XML files that reference the DTD file. To be considered a valid XML file, the document must be accompanied by a DTD (or an XML schema), and conform to all of the declarations in the DTD (or XML schema).


DTD - Syntax

Syntax
<!DOCTYPE element DTD identifier
[
   declaration1
   declaration2
   ........
]> 

Internal DTD

Syntax
<!DOCTYPE root-element [element-declarations]> 

External DTD

Syntax
<!DOCTYPE root-element SYSTEM "file-name"> 

Anatomy of a DTD

Generally, DTDs consist of three basic parts:

  • Elements declarations
  • Attributes declarations
  • Entities declarations

DTD - Element Declarations

When using a DTD to define the content of an XML document, you must declare each element that appears within the document.


Syntax

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>  

The ELEMENT declaration begins with an exclamation mark. Following the ELEMENT keyword is the name of the element that you are defining. The element name must appear exactly as it will within the XML document, including any namespace prefix. The content model of the element appears after the element name. An element may contain element children, text, a combination of children and text, or the element may be empty.

Empty Elements


Syntax
<!ELEMENT element-name EMPTY> 

Elements with Parsed Character Data


Syntax
<!ELEMENT element-name (#PCDATA)>  

Elements with any Contents


Syntax
<!ELEMENT element-name ANY> 

Elements with Children (sequences)


Syntax
<!ELEMENT element-name (child-name1,child-name2,... )> 

Declaring Minimum One Occurrence of an Element


Syntax
<!ELEMENT element-name (child-name+)> 

Declaring Zero or More Occurrences of an Element


Syntax
<!ELEMENT element-name (child-name*)> 

Declaring Zero or One Occurrences of an Element


Syntax
<!ELEMENT element-name (child-name?)>

Declaring either/or Content


Syntax
<!ELEMENT element-name (child-name1|child-name2)> 

Declaring Mixed Content


Syntax
<!ELEMENT element-name (#PCDATA|child-name1|child-name2)*> 

DTD - Attributes declarations

Attribute declarations are similar to element declarations in many ways. Instead of declaring allowable content models for elements, you declare a list of allowable attributes for each element. These lists are called ATTLIST declarations:


Syntax
<!ATTLIST element-name attribute-name attribute-type attribute-value> 

The attribute-type can be one of the following:

Type Description
CDATA The value is character data
(en1|en2|..) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation

The attribute-value can be one of the following:

Value Description
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is optional
#FIXED value The attribute value is fixed

DTD - Entities declarations

Entities are used to define shortcuts to special characters within the XML documents. Entities can be primarily of four types

  • Built-in entities
  • Character entities
  • General entities

Built-in entities

All XML parsers must support built-in entities. In general, you can use these entity references anywhere. You can also use normal text within the XML document, such as in element contents and attribute values.

There are five built-in entities can be used within an XML document by default:

  • &amp; —The & character
  • &lt; —The < character
  • &gt; —The > character
  • &apos; —The ‘ character
  • &quot; —The “ character

Character entities

Character entities, much like the five built-in entities, are not declared within the DTD. Instead, they can be used in the document within element and attribute content without any declaration. You can used &#169; as value for copyright character. The browser will see that copyright is replaced by the character ©.


General entities

Instead of representing only a single character, general entities can represent characters, paragraphs, and even entire documents.

Syntax
<!ENTITY entity_name "text"> 

Example
<!DOCTYPE note [
   <!ENTITY CSE "Computer Science and Engineering">
]>
<department> &CSE; </department> 

Note: In an instance document, when the XML parser finds &CSE;, it replaces it with Computer Science and Engineering.



DTD-Internal-Example

<!DOCTYPE department [
   <!ELEMENT department (student)>
   <!ELEMENT student (name,rollno)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT rollno (#PCDATA)>
]>

<department>
  <student>
    <name>.....</name>
	<rollno>....</rollno>
  </student>
</department> 

DTD-External-Example

department.xml
<!DOCTYPE department SYSTEM "student.dtd">
<department>
  <student rollno="501">
    <name>Ramu</name>
	<branch>&CSE;</branch>
  </student>
  <student rollno="401">
    <name>Arun</name>
	<branch>&ECE;</branch>
  </student>
</department> 
student.dtd
<!ELEMENT department (student+)>
   <!ELEMENT student (name,branch)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT branch (#PCDATA)>
   <!ATTLIST student rollno CDATA #REQUIRED>
   <!ENTITY CSE "Computer Science and Engineering">	
   <!ENTITY ECE "Electronics and Communications Engineering">

Next Topic :XML Schema