← New search

Other meanings of Document type definition

Computing

Document type definition

A document type definition (DTD) is a set of markup declarations that define the structure, elements, and attributes of a document type in the Standard Generalized Markup Language (SGML) family, most notably XML and HTML. It serves as a formal grammar, specifying which elements may appear, in what order, and what content they may contain. DTDs originated in SGML and were adopted into XML 1.0 as an optional component. They are used for validation, ensuring that documents conform to a defined schema, and for defining entities and notations. Although newer schema languages like XML Schema and RELAX NG offer more expressive power, DTDs remain widely used due to their simplicity, compact syntax, and support in legacy systems.

1986
Year SGML standard (ISO 8879) introduced DTDs
Origin
1998
Year XML 1.0 incorporated DTDs
XML adoption
5
Number of DTD declarations: ELEMENT, ATTLIST, ENTITY, NOTATION, COMMENT
Declaration types
2
Ways to include a DTD: internal subset and external subset
Inclusion methods
1

Core syntax and validation

A DTD consists of declarations that define element types, attribute lists, entities, and notations. The <!ELEMENT> declaration specifies the element name and its content model, which can be empty, any, or a sequence/choice of child elements with occurrence indicators (?, *, +). For example, <!ELEMENT note (to, from, body)> requires that a note element contain exactly one to, one from, and one body in that order. The <!ATTLIST> declaration defines attributes for an element, including their types (CDATA, ID, IDREF, etc.) and default values. Validation against a DTD is performed by a validating parser, which checks that the document's structure and attributes conform to the declared rules. This process is essential for data interchange, as it ensures that documents from different sources adhere to a common structure.1

2

Internal and external subsets

A DTD can be included directly within an XML document as an internal subset, placed in the document type declaration (<!DOCTYPE root-element [...]>), or referenced externally via a system identifier or public identifier. External DTDs are stored in separate files with a .dtd extension and can be shared across many documents, promoting consistency and maintainability. The internal subset can override or augment the external subset, allowing for document-specific customizations. This dual mechanism was inherited from SGML and is a key feature of XML's modularity. However, external DTDs pose a security risk known as XXE (XML External Entity) attacks, where malicious entities can read local files or trigger network requests; mitigations include disabling external entity resolution in parsers.2

3

Entities and notations

DTDs define general entities (used as &name; in content) and parameter entities (used in the DTD itself) via <!ENTITY> declarations. These entities can be internal (with a literal replacement string) or external (referencing an external file, often for non-XML data like images). Notations, declared with <!NOTATION>, identify the format of unparsed external entities, such as binary files, and are used in conjunction with entity declarations that have an NDATA type. This capability allows DTDs to incorporate non-XML content, a feature that is often overlooked but was crucial in SGML for integrating multimedia. For example, an image entity might be declared as <!ENTITY logo SYSTEM "logo.gif" NDATA GIF>, with the GIF notation defined separately.3

4

Lesser-known aspects

DTDs have several niche features and historical quirks. One is the IGNORE and INCLUDE conditional sections, which allow parts of the DTD to be conditionally processed, useful for maintaining multiple versions. Another is the #FIXED attribute default, which forces an attribute to have a constant value, often used for versioning or namespace declarations. DTDs also support ID and IDREF attribute types, enabling cross-references within a document, a precursor to XLink. In HTML, the DTD is famously used for doctype switching: the presence of a DTD triggers standards mode in browsers, while its absence triggers quirks mode. The XHTML 1.0 DTDs are still referenced by many legacy pages. Additionally, DTDs are not namespace-aware, which led to the development of namespace-aware schema languages; however, DTDs remain the only schema language that can define entities, a limitation of XML Schema.4

Glossary

SGML
Standard Generalized Markup Language, an ISO standard for defining markup languages.
XML
Extensible Markup Language, a simplified subset of SGML.
Entity
A named unit of content that can be referenced in a document, such as a character or file.
Notation
A declaration that identifies the format of unparsed external entities.
Validating parser
A parser that checks a document against a DTD or schema.

DTDs remain a cornerstone of markup language design, balancing simplicity with expressive power.