Topics

Next topics

Step Four: Including a Document Type Declaration

The goal of this step is including a document type declaration in your document, which will link to or include your DTD. The document type declaration names the document type being used and links to or includes its definition, the DTD.

The document type declaration, which is situated after the XML declaration, is a mechanism for naming the document type to which a document complies and pointing to or including its definition. Valid XML documents must declare the document type to which they comply and provide their complete definition so that editors, browsers and converters can read its DTD to understand it. Well-formed documents can also include a document type declaration and include markup declarations in its external subset but are not required to do so. The document type declaration names the document type by making reference to the root element of the document. It can make reference to an external DTD, called the external DTD subset, include the DTD internally in the internal DTD subset or use both. Document type declarations take the general form: <!DOCTYPE NAME SYSTEM "file"[]>.

The use of the document type declaration is variable, depending on the DTD subsets used to contain the DTD. The DTD can be housed exclusively by either the external or internal subsets, or in both. Each of these options can use a slightly different syntax. The simplest declaration, which allows only an external subset of the DTD follows: <!DOCTYPE NAME SYSTEM "file">. The following declaration allows the use of any or both subsets and is the most common: <!DOCTYPE NAME SYSTEM "file" [ ]>. The square brackets house the internal subset. The last declaration only allows the internal subset: <!DOCTYPE NAME [ ]>.

If the root element of a particular DTD were poem then the document type declaration would read <!DOCTYPE poem []>. The definition of the poem document type would have to be included in the internal DTD subset, marked by the [ and ] markers. This definition could also be saved as an external file and linked to by the document type declaration in the following fashion: <!DOCTYPE poem SYSTEM "poem.dtd">.

Your Valid XML Instance

You should add a document type declaration to your XML document, that names your document type, might point to the external subset or include an internal subset, which is marked by the [ and ] markers. This example has named the document type as MEMO, does not point to an external subset, but includes markup declarations in the internal subset. The DTD could just as easily have been stored as an external file and used the following document type declaration: <!DOCTYPE MEMO SYSTEM "memo.dtd">.

<?xml version="1.0" standalone="no"?>

<!DOCTYPE MEMO [
<!ELEMENT MEMO     (TO,FROM,SUBJECT,BODY,SIGN)>
<!ATTLIST MEMO     importance   (HIGH|MEDIUM|LOW) "LOW">
<!ELEMENT TO       (#PCDATA)>
<!ELEMENT FROM     (#PCDATA)>
<!ELEMENT SUBJECT  (#PCDATA)>
<!ELEMENT BODY     (P+)>
<!ELEMENT P        (#PCDATA)>
<!ELEMENT SIGN     (#PCDATA)>
<!ATTLIST SIGN     signatureFile CDATA #IMPLIED
                   email         CDATA #REQUIRED>
]>

Document Type Declaration Syntax

This definition provides explanation for the various forms of document type declaration syntax.

Term
Meaning
<!DOCTYPE
Starts the document type declaration
NAME
Names the document type being defined, which must comply with XML NAME rules
SYSTEM
Indicates that a system identifier, which follows, must be read and resolved
"report.dtd"
A quoted system identifier, which must be resolved to locate the DTD
[
Starts the internal subset
The internal DTD subset, which can contain any number of markup declarations
]
Ends the internal subset
>
Ends the document type declaration