DOCTYPE Declaration & DTDs
The document type (DOCTYPE) declaration consists of an internal, or references an external Document Type Definition (DTD). It can also have a combination of both internal and external DTDs. The DTD defines the constraints on the structure of an XML document. It declares all of the document's element types , children element types, and the order and number of each element type. It also declares any attributes, entities, notations, processing instructions, comments, and PE references in the document.
The Internal DTD:
<!DOCTYPE root_element [
Document Type Definition (DTD):
elements/attributes/entities/notations/
processing instructions/comments/PE references
]>
|
Example: |
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE foo [
<!ELEMENT foo (#PCDATA)>
]>
<foo>Hello World.</foo>
|
Rules:
The External DTD:
External DTDs are useful for creating a common DTD that can be shared between multiple documents. Any changes that are made to the external DTD automatically updates all the documents that reference it. There are two types of external DTDs: private, and public.
Rules:
- If any elements, attributes, or entities are used in the XML document that are referenced or defined in an external DTD, standalone="no" must be included in the XML declaration
.
"Private" External DTDs:
Private external DTDs are identified by the keyword SYSTEM, and are intended for use by a single author or group of authors.
<!DOCTYPE root_element SYSTEM "DTD_location">
|
where:
- DTD_location: relative or absolute URL

Example: |
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE document SYSTEM "subjects.dtd">
<document>
<title>Subjects available in Mechanical Engineering.</title>
<subjectID>2.303</subjectID>
<subjectname>Fluid Mechanics</subjectname>
<prerequisite>
<subjectID>1.001</subjectID>
<subjectname>Mathematics</subjectname>
</prerequisite>
<classes>4 hours per week (lectures and tutorials) for one
semester.</classes>
<assessment>tutorial assignments and one 2hr exam at end of
course.</assessment>
<syllabus>
Fluid statics. The Bernoulli equation. Energy equation. Momentum
equation. Differential Continuity equation. Differential Energy
equation. Differential Momentum equation. Dimensional Analysis.
Similitude. Laminar flow. Turbulent flow. Lift and Drag. Boundary
layer theory.
</syllabus>
<textbooks>
<author>Foobar</author>
<booktitle>The Study of Fluid Mechanics</booktitle>
</textbooks>
</document>
|
The external DTD ("subjects.dtd") referenced in the example above contains information about the XML document's structure:
subjects.dtd:
|
<!ELEMENT document
(title*,subjectID,subjectname,prerequisite?,
classes,assessment,syllabus,textbooks*)>
<!ELEMENT prerequisite (subjectID,subjectname)>
<!ELEMENT textbooks (author,booktitle)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT subjectID (#PCDATA)>
<!ELEMENT subjectname (#PCDATA)>
<!ELEMENT classes (#PCDATA)>
<!ELEMENT assessment (#PCDATA)>
<!ATTLIST assessment assessment_type (exam | assignment) #IMPLIED>
<!ELEMENT syllabus (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT booktitle (#PCDATA)>
|
"Public" External DTDs:
Public external DTDs are identified by the keyword PUBLIC and are intended for broad use. The "DTD_location" is used to find the public DTD if it cannot be located by the "DTD_name".
<!DOCTYPE root_element PUBLIC "DTD_name" "DTD_location">
|
where:
- DTD_location: relative or absolute URL

- DTD_name: follows the syntax:
"prefix//owner_of_the_DTD//
description_of_the_DTD//ISO 639_language_identifier"
|
Example: |
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>A typical HTML file</TITLE>
</HEAD>
<BODY>
This is the typical structure of an HTML file. It follows
the notation of the HTML 4.0 specification, including tags
that have been deprecated (hence the "transitional" label).
</BODY>
</HTML>
|
Note:
The following prefixes are allowed in the DTD name:
Prefix: |
Definition: |
ISO |
The DTD is an ISO standard. All ISO standards are approved. |
+ |
The DTD is an approved non-ISO standard. |
- |
The DTD is an unapproved non-ISO standard. |
Combining Internal and External Document Type Definitions:
A document can use both internal and external DTD subsets. The internal DTD subset is specified between the square brackets of the DOCTYPE declaration. The declaration for the external DTD subset is placed before the square brackets immediately after the SYSTEM keyword.
Example: |
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE document SYSTEM "subjects.dtd" [
<!ATTLIST assessment assessment_type (exam | assignment | prac)>
<!ELEMENT results (#PCDATA)>
]>
|
subjects.dtd: |
<!ELEMENT document
(title*,subjectID,subjectname,prerequisite?,
classes,assessment,syllabus,textbooks*)>
<!ELEMENT prerequisite (subjectID,subjectname)>
<!ELEMENT textbooks (author,booktitle)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT subjectID (#PCDATA)>
<!ELEMENT subjectname (#PCDATA)>
<!ELEMENT classes (#PCDATA)>
<!ELEMENT assessment (#PCDATA)>
<!ATTLIST assessment assessment_type (exam | assignment) #IMPLIED>
<!ELEMENT syllabus (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT booktitle (#PCDATA)>
|
Rules:
- Declaring an ELEMENT with the same name in both the internal and external DTD subsets is invalid
.
|