A document type definition (DTD) declares the elements, attributes, entities and notations
of a class of documents. It is optional; a document with no DTD can still be
well-formed, but only a document with a DTD can be valid. Productions are
from XML 1.0. External files of declarations
usually have the extension .dtd or, for entities, .ent and
.mod.
The document type declaration [28]
<!DOCTYPE note SYSTEM "note.dtd" [
<!ELEMENT note (to, from, body)>
<!ELEMENT to (#PCDATA)>
]>
The name must be that of the root element (validity constraint
Root Element Type). The optional external identifier [75] points to the
external subset, either SYSTEM "uri" or PUBLIC
"public id" "uri"; the optional part in brackets is the internal
subset, read first, so its declarations win over those in the external
subset where both are allowed to bind.
- In the internal subset, parameter entity references may appear only between declarations (WFC: PEs in Internal Subset).
- The external subset [30] may use parameter entities anywhere, and conditional sections.
- A processor does not have to read an external subset. This site’s viewer does not; a file’s validity is only reported when the internal subset is complete.
Element declarations [45]
<!ELEMENT name contentspec>. The content specification is one of:
| Form | Meaning |
|---|---|
EMPTY | No content; the element must be <name/> or have nothing between its tags |
ANY | Anything, from declared elements |
(#PCDATA) | Text only (mixed content [51]) |
(#PCDATA | a | b)* | Text mixed with the listed elements in any order |
(a, (b | c)*, d?) | Children: a content model of sequences , and choices |, with ? (optional), * (zero or more) and + (one or more) |
Content models must be deterministic for compatibility with SGML (appendix E), so a parser can tell which part matches without looking ahead. An element type must not be declared twice (VC: Unique Element Type Declaration).
Attribute lists [52]
<!ATTLIST element name type default …>. Several attribute lists may
exist for one element; the first declaration of an attribute name binds.
| Type [54]–[59] | Value |
|---|---|
CDATA | Any text |
ID | A Name unique in the document; an element type has at most one ID attribute |
IDREF, IDREFS | The ID of another element, or several |
ENTITY, ENTITIES | The name of an unparsed entity, or several |
NMTOKEN, NMTOKENS | A name token, or several |
NOTATION (a | b) | One of the listed notation names; an element type has at most one |
(a | b | c) | One of the listed name tokens (an enumeration) |
| Default [60] | Meaning |
|---|---|
#REQUIRED | Must be given on every element |
#IMPLIED | Optional, no default |
"value" | Optional, with this default |
#FIXED "value" | Optional, but if present must equal this value |
Attribute values are normalised (3.3.3): tabs and line ends become spaces, and for all types other than CDATA, runs of spaces collapse and leading and trailing spaces are removed. A value is checked against its type only for validity.
Notations [82]
<!NOTATION name SYSTEM "uri"> or PUBLIC "public id"
names a format for unparsed entities, or for processing instruction targets. Notation
names must be declared once (VC: Unique Notation Name), and must exist where an
unparsed entity or NOTATION attribute uses them (VC: Notation Declared).
Conditional sections [61]–[65]
<![INCLUDE[ … ]]> and <![IGNORE[ … ]]> switch
declarations on and off, usually with a parameter entity for the keyword. They are
allowed only in the external subset and external parameter entities, and may nest.
Standalone documents
standalone="yes" in the XML declaration promises that no declarations
outside the document (in the external subset or external parameter entities) affect
what the document says: no attribute defaults, no entity declarations used by it, no
normalisation of attribute values, and no white-space-sensitive element content
depend on them. It only constrains documents with an external DTD, and is a validity
constraint, not a well-formedness one.
Validity
A validating processor reads the DTD and checks the document against it: every validity constraint. A non-validating processor may skip the external subset and only checks well-formedness. Both must report well-formedness errors; only validating processors report validity errors.