A DTD is a set of markup declarations, broken up into two subsets.
- Internal DTD Subset
- External DTD Subset
Even thought the XML recommendation uses the term subset in some case the order of declarations is important unlike a typical mathematical set.
If the elements are declared inside the XML files then it can be referred as Internal DTD. In order to refer a file as internal DTD, the attribute standalone in XML declaration should be set to
yes. the internal subset of markup declarations is enclosed in square brackets
[ ].
Syntax:
[xml]
<!DOCTYPE root-element [element-declarations]>
[/xml]
Example: Below code is an example of Internal DTD Subset in which the attribute
standalone
is set to "Yes" by declaring that DTD is declared inside the XML itself.
[xml]
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE address [
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>Andy Murray</name>
<company>SPLessons</company>
<phone>(011) 123-4567</phone>
</address>
[/xml]
If the elements are declared outside the XML files then it can be referred as External DTD. The external subset is identified by a system or public identifier which may be either the legal
.dtd
file or a valid URL. In order to refer a file as external DTD, the attribute standalone in XML declaration should be set to
no.
Syntax:
[xml]
<!DOCTYPE root-element SYSTEM "file-name">
[/xml]
In the above syntax, file name should be saved with .dtd extension.
Example: Below code is an example of External DTD Subset in which the attribute
standalone
is set to "no" by declaring that DTD is declared outside of the XML document and is retrieved with the help of
SYSTEM
Identifier.
[xml]
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE address SYSTEM "contact.dtd">
<contact>
<name>Andy Murray</name>
<company>SPLessons</company>
<phone>(011) 123-4567</phone>
</contact>
[/xml]
The file
contact.dtd
will have the below code.
[xml]
<!ELEMENT contact (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
[/xml]