A DTD is a set of rules that defines what tags appear in a XML document, what
attributes the tags may have and what a relationship the tags have with each other. When
an XML document is processed, it is compared within the DTD to be sure it is structured
correctly and all tags are used in the proper manner. This comparison process is called
validation and it is performed by a tool called parser.Example for a DTD (This is a shortened version from the DTD of our sample XML
document):
film.dtd
<!DOCTYPE film [
<!ENTITY COM "Comedy">
<!ENTITY SF "Science Fiction">
<!ELEMENT film (title+,genre,year)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title
xml:lang NMTOKEN
"EN"
id ID #IMPLIED>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT year (#PCDATA)>
]>
|
Remember, a DTD is only needed for a valid XML
document. A DTD is also useful for checking a XML document for structural errors.
1. Internal DTDs
Internal DTD (markup declaration) are inserted within
the doctype declaration. DTDs inserted this way are used in the that specific
document. This might be the approach to take for the use of a small number of tags
in a single document, as in this example:
<?xml version="1.0"?>
<!DOCTYPE film [
<!ENTITY COM "Comedy">
<!ENTITY SF "Science Fiction">
<!ELEMENT film (title+,genre,year)>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title
xml:lang NMTOKEN
"EN"
id ID #IMPLIED>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT year (#PCDATA)>
]>
<film>
<title id="1">Tootsie</title>
<genre>&COM;</genre>
<year>1982</year>
<title id="2">Jurassic Park</title>
<genre>&SF;</genre>
<year>1993</year>
</film>
|
2. External DTD
DTDs can be very complex and creating a DTD requires a
certain amount of work. DTDs are stored as ASCII text files with the extension
'.dtd'. In the following
example we assume, that the previously internal DTD was saved as a separate file (under
the name film.dtd), and is therefore now referred to as external
definition (external DTD):
<?xml version="1.0"?>
<!DOCTYPE film SYSTEM
"film.dtd">
<film>
<title id="1">Tootsie</title>
<genre>&COM;</genre>
<year>1982</year>
<title id="2">Jurassic Park</title>
<genre>&SF;</genre>
<year>1993</year>
</film>
|
For making the creation of XML documents
easier, there are published DTD that define tags for commonly used elements. This avoids
the recreation of existing DTD by merely pointing to them in the doctype tag of the XML
file. Furthermore these XML rules pointed to must be followed when creating the XML
document.
3. Shared DTDs
The document type declaration can point to
an external subset (a special kind of external entity) containing markup declarations, or
can contain the markup declarations directly in an internal subset, or can do both. The
DTD for a document consists of both subsets taken together.
When both the external and internal subsets are used, the
internal subset is considered to occur before the external subset. This has the effect
that entity and attribute-list declarations in the internal subset take precedence over
those in the external subset.
There are four kinds of markup declarations
in XML within the DTD:
- element declarations
- attribute list declarations
- entity declarations, and
- notation declarations
You should try to be as conservative as the DTD allows you
to be, when declaring elements, attributes, entities and notations. We will now have a look at DTDs in detail. |