A document type definition (DTD) is a specification file that contains a set of markup declarations that define a document type for an SGML-family markup language (GML, SGML, XML, HTML). The DTD specification file can be used to validate documents.

A DTD defines the valid building blocks of an XML document. It defines the document structure with a list of validated elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference.

DTDs persist in applications that need special publishing characters, such as the XML and HTML Character Entity References, which derive from larger sets defined as part of the ISO SGML standard effort. XML uses a subset of SGML DTD. An attempt to create namespace-aware DTDs was being developed as Part 9 of ISO DSDL, but this was ultimately withdrawn.

, newer XML namespace-aware schema languages (such as W3C XML Schema and ISO RELAX NG) have largely superseded DTDs as a better way to validate XML structure.

Associating DTDs with documents A DTD is associated with an XML or SGML document by means of a document type declaration (DOCTYPE). The DOCTYPE appears in the syntactic fragment doctypedecl near the start of an XML document. The declaration establishes that the document is an instance of the type defined by the referenced DTD.

DOCTYPEs make two sorts of declarations: an optional external subset an optional internal subset.

The declarations in the internal subset form part of the DOCTYPE in the document itself. The declarations in the external subset are located in a separate text file. The external subset may be referenced via a public identifier and/or a system identifier. Programs for reading documents may not be required to read the external subset.

Any valid SGML or XML document that references an external subset in its DTD, or whose body contains references to parsed external entities declared in its DTD (including those declared within its internal subset), may only be partially parsed but cannot be fully validated by validating SGML or XML parsers in their standalone mode (this means that these validating parsers do not attempt to retrieve these external entities, and their replacement text is not accessible).

However, such documents are still fully parsable in the non-standalone mode of validating parsers, which signals an error if it can not locate these external entities with their specified public identifier (FPI) or system identifier (a URI), or are inaccessible. (Notations declared in the DTD are also referencing external entities, but these unparsed entities are not needed for the validation of documents in the standalone mode of these parsers: the validation of all external entities referenced by notations is left to the application using the SGML or XML parser). Non-validating parsers may eventually attempt to locate these external entities in the non-standalone mode (by partially interpreting the DTD only to resolve their declared parsable entities), but do not validate the content model of these documents.

Examples The following example of a DOCTYPE contains both public and system identifiers:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

All HTML 4.01 documents conform to one of three SGML DTDs. The public identifiers of these DTDs are constant and are as follows: -//W3C//DTD HTML 4.01//EN -//W3C//DTD HTML 4.01 Transitional//EN -//W3C//DTD HTML 4.01 Frameset//EN

The system identifiers of these DTDs, if present in the DOCTYPE, are URI references. A system identifier usually points to a specific set of declarations in a resolvable location. SGML allows mapping public identifiers to system identifiers in catalogs that are optionally available to the URI resolvers used by document parsing software.

This DOCTYPE can only appear after the optional XML declaration, and before the document body, if the document syntax conforms to XML. This includes XHTML documents:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- the XHTML document body starts here--> <html xmlns="http://www.w3.org/1999/xhtml"> ... </html>

An additional internal subset can also be provided after the external subset:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [ <!-- an internal subset can be embedded here --> ]> <!-- the XHTML document body starts here--> <html xmlns="http://www.w3.org/1999/xhtml"> ... </html>

Alternatively, only the internal subset may be provided:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html [ <!-- an internal subset can be embedded here --> ]> <!-- the XHTML document body starts here--> <html xmlns="http://www.w3.org/1999/xhtml"> ... </html>

Finally, the document type definition may include no subset at all; in that case, it just specifies that the document has a single top-level element (this is an implicit requirement for all valid XML and HTML documents, but not for document fragments or for all SGML documents, whose top-level elements may be different from the implied root element), and it indicates the type name of the root element: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html> <!-- the XHTML document body starts here--> <html xmlns="http://www.w3.org/1999/xhtml"> ... </html>

Markup declarations DTDs describe the structure of a class of documents via element and attribute-list declarations. Element declarations name the allowable set of elements within the document, and specify whether and how declared elements and runs of character data may be contained within each element. Attribute-list declarations name the allowable set of attributes for each declared element, including the type of each attribute value, if not an explicit set of valid values.

DTD markup declarations declare which element types, attribute lists, entities, and notations are allowed in the structure of the corresponding class of XML documents.

Element type declarations An element type declaration defines an element and its possible content. A valid XML document contains only elements that are defined in the DTD.

Various keywords and characters specify an element's content: EMPTY for specifying that the defined element allows no content, i.e., it cannot have any children elements, not even text elements (if there are whitespaces, they are ignored); ANY for specifying that the defined element allows any content, without restriction, i.e., that it may have any number (including none) and type of children elements (including text elements); or an expression, specifying the only elements allowed as direct children in the content of the defined element; this content can be either: a mixed content, which means that the content may include at least one text element and zero or more named elements, but their order and number of occurrences cannot be restricted; this can be: ( #PCDATA ): historically meaning parsed character data, this means that only one text element is allowed in the content (no quantifier is allowed); ( #PCDATA | ''element name'' | ... )*: a limited choice (in an exclusive list between parentheses and separated by "|" pipe characters and terminated by the required "*" quantifier) of two or more child elements (including only text elements or the specified named elements) may be used in any order and number of occurrences in the content. an element content, which means that there must be no text elements in the children elements of the content (all whitespaces encoded between child elements are then ignored, just like comments). Such element content is specified as content particle in a variant of Backus–Naur form without terminal symbols and element names as non-terminal symbols. Element content consists of: a content particle can be either the name of an element declared in the DTD, or a sequence list or choice list. It may be followed by an optional quantifier. a sequence list means an ordered list (specified between parentheses and separated by a "," comma character) of one or more content particles: all the content particles must appear successively as direct children in the content of the defined element, at the specified position and relative order; a choice list means a mutually exclusive list (specified between parentheses and separated by a "|" pipe character) of two or more content particles: only one of these content particles may appear in the content of the defined element at the same position. A quantifier is a single character that immediately follows the specified item it applies to, to restrict the number of successive occurrences of these items at the specified position in the content of the element; it may be either: + for specifying that there must be one or more occurrences of the item — the effective content of each occurrence may be different; * for specifying that any number (zero or more) of occurrences is allowed — the item is optional and the effective content of each occurrence may be different; ? for specifying that there must not be more than one occurrence — the item is optional; If there is no quantifier, the specified item must occur exactly one time at the specified position in the content of the element.

For example: <!ELEMENT html (head, body)> <!ELEMENT p (#PCDATA | p | ul | dl | table | h1|h2|h3)*>

Element type declarations are ignored by non-validating SGML and XML parsers (in which cases, any elements are accepted in any order, and in any number of occurrences in the parsed document), but these declarations are still checked for form and validity.

Attribute list declarations An attribute list specifies for a given element type the list of all possible attribute associated with that type. For each possible attribute, it contains: the declared name of the attribute, its data type (or an enumeration of its possible values), and its default value.Attribute-list Declaration, Specifications of Extensible Markup Language (XML) 1.1, W3C.

For example: <!ATTLIST img src CDATA #REQUIRED id ID #IMPLIED sort CDATA #FIXED "true" print (yes | no) "yes" >

Here are some attribute types supported by both SGML and XML: CDATA this type means characters data and indicates that the effective value of the attribute can be any textual value, unless the attribute is specified as fixed (the comments in the DTD may further document values that are effectively accepted, but the DTD syntax does not allow such precise specification); ID the effective value of the attribute must be a valid identifier, and it is used to define and anchor to the current element the target of references using this defined identifier (including as document fragment identifiers that may be specified at end of an URI after a "#" sign); it is an error if distinct elements in the same document are defining the same identifier; the uniqueness constraint also implies that the identifier itself carries no other semantics and that identifiers must be treated as opaque in applications; XML also predefines the standard pseudo-attribute "xml:id" with this type, without needing any declaration in the DTD, so the uniqueness constraint also applies to these defined identifiers when they are specified anywhere in a XML document. IDREF or IDREFS the effective value of the attribute can only be a valid identifier (or a space-separated list of such identifiers) and must be referencing the unique element defined in the document with an attribute declared with the type ID in the DTD (or the unique element defined in an XML document with a pseudo-attribute "xml:id") and whose effective value is the same identifier;