XML Document Type Declaration, commonly it is called DTD. It is used to describe XML language absolutely. When we create DTD, we are creating a syntax rules for any XML documents that uses the DTD.
Syntax
1 2 3 4 5 6 7 8 |
<! DOCTYPE element DTD identifier [ decleration1 decleration2 . . . . . ]> |
- It start with <! DOCTYPE delimiter.
- An element tells parser to parse document from specified root element.
Internal DTD
A DTD is referred to as an internal DTD if elements are declared within the XML files. Here declaration works independent of an external source.
Syntax
1 2 3 |
<! DOCTYPE root-element [element-declarations]> |
Where root-element is the name of root element and element declaration is where you declare the elements.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version=?1.0??> <! DOCTYPE student[ <!ELEMENT students (name, class, section)> <!ELEMENT name (#PCDATA)> <!ELEMENT class (#PCDATA)> <!ELEMENT section (#PCDATA)> ]> <student> <name> Rohit sharma</name> <class>10</class> <section>?B?</section> </student> |
External DTD
In external DTD elements are declared outside the xml file. The URL can point to either a local or remote file using relative and absolute refrencesrespectively. It means declaration includes information from the external source.
Syntax
1 2 3 |
<! DOCTYPE root-element SYSTEM "file-name"> |
Where file name is the file with .dtd extension.
Example
Example shows external DTD usage:
1 2 3 4 5 6 7 8 9 |
<?xml version=?1.0??> <!DOCTYPE student SYSTEM "student.dtd"> <student> <name> Rohit sharma</name> <class>10</class> <section>?B?</section> </student> |
The content of the DTD file student.dtd is as shown:
1 2 3 4 5 6 |
<!ELEMENT student (name, class, section)> <!ELEMENT name (#PCDATA)> <!ELEMENT class (#PCDATA)> <!ELEMENT section (#PCDATA)> |
We use system identifiers or public identifiers to indicate an external DTD.
System identifiers
It enable us to specify the location of an external file containing DTD declarations.
1 2 3 |
<!DOCTYPE name SYSTEM "address.dtd" [. . . .]> |
It contains keyword SYSTEM and a URI reference pointing to the location of the document.
Public identifiers
It provide the mechanism to locate DTD resources.
1 2 3 |
<!DOCTYPE name PUBLIC "-//Beginning XML// DTD Address Example//EN"> |
Here we can see, it begins with keyword PUBLIC, followed by specialized identifier. Public identifiers are used to identify an entry in a catalog. It follow any format, commonly used format is called Formal public identifiers.