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
Internal DTD
Syntax
External DTD
Syntax
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
or
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
Elements with Parsed Character Data
Syntax
Elements with any Contents
Syntax
Elements with Children (sequences)
Syntax
Declaring Minimum One Occurrence of an Element
Syntax
Declaring Zero or More Occurrences of an Element
Syntax
Declaring Zero or One Occurrences of an Element
Syntax
Declaring either/or Content
Syntax
Declaring Mixed Content
Syntax
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
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:
& —The & character < —The < character > —The > character ' —The ‘ character " —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 © 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
Example
]>
&CSE;
Note: In an instance document, when the XML parser finds &CSE;, it replaces it with Computer Science and Engineering.
DTD-Internal-Example
]>
.....
....
DTD-External-Example
Ramu
&CSE;
Arun
&ECE;