Document type definition
A document type definition (DTD) is a set of markup declarations that defines the structure, legal elements, and attributes of an XML or SGML document. DTDs serve as schema languages that specify the document type's grammar and constraints, enabling validation of document instances against predefined rules.
Historical background and purpose
DTDs originated with Standard Generalized Markup Language (SGML) in the 1980s and were later adopted by XML. Their primary purpose is to define the building blocks of an XML document, establishing what elements must be present, their permissible order, and how they relate to one another. DTDs enable document validation, ensuring that XML documents conform to a specific structure required by applications or standards. This validation capability made DTDs particularly important in early web development, where they were used to validate HTML documents.
Basic structure and components
A DTD consists of declarations that define elements, attributes, entities, and notations. Element declarations specify the names of elements and their content models using specialized syntax. Attribute declarations define the attributes associated with elements, including their data types and default values. Entity declarations create shortcuts for frequently used text or special characters. Notations describe the format of non-XML data. These declarations can be embedded within the XML document itself (internal subset) or stored in external files (external subset) that can be shared across multiple documents.
Element declarations
Element declarations form the core of a DTD, defining what elements may appear in a document and their hierarchical relationships. The declaration syntax uses the ELEMENT keyword followed by the element name and its content specification. Content models can be EMPTY (no content), ANY (any content allowed), mixed content (text and child elements), or element content (only child elements). Content models use parentheses and occurrence indicators to specify sequences, choices, and repetition of child elements. For example, the declaration <!ELEMENT book (title, author+, chapter*)> defines a book element that must contain one title, at least one author, and zero or more chapters in that specific order.
Attribute declarations
Attribute declarations specify the characteristics or properties of elements using the ATTLIST keyword. Each attribute declaration identifies the element name, followed by attribute name-value pairs that define the attribute's characteristics. DTDs support several attribute types including CDATA (character data), enumerated values, ID (unique identifier), IDREF (reference to an ID), and NMTOKEN (name token). Attribute defaults can be specified as #REQUIRED (must be present), #IMPLIED (optional), #FIXED (constant value), or a literal default value. For instance, <!ATTLIST book isbn ID #REQUIRED category (fiction|nonfiction) "fiction"> declares that every book element must have a unique isbn identifier and may have a category attribute that defaults to "fiction".
Entity declarations
Entities in DTDs function as substitution mechanisms, allowing the definition of reusable content fragments. General entities are used within document content, while parameter entities operate within the DTD itself. Entity declarations use the ENTITY keyword followed by the entity name and its replacement text or external resource reference. Predefined entities in XML include & (&), < (<), > (>), " ("), and ' ('). Custom entities can be defined for frequently used text, special characters, or external file inclusions. For example, <!ENTITY copyright "© 2023 Company Name"> creates an entity that can be referenced as ©right; throughout the document.
Internal and external DTDs
DTDs can be implemented as internal subsets, external subsets, or combinations of both. Internal DTDs are declared within the XML document's DOCTYPE declaration, making them specific to that document. External DTDs are stored in separate files with a .dtd extension and can be referenced by multiple XML documents, promoting consistency and reusability. The SYSTEM keyword references a private DTD file, while the PUBLIC keyword points to a publicly registered DTD. Documents can use both internal and external DTDs, with the internal subset having precedence in case of conflicts.
Validation and processing
XML processors can validate documents against their DTDs to ensure structural conformity. During validation, the processor checks that all elements appear in the correct order and hierarchy, required attributes are present, element content matches declared content models, and entity references are properly resolved. Validation helps maintain data integrity and interoperability between systems. While validation is optional in XML processing, it is crucial for applications requiring strict document structure, such as in publishing systems, data exchange formats, and configuration files.
Limitations and alternatives
Despite their historical importance, DTDs have several limitations. They lack support for data typing beyond basic strings and enumerations, provide no namespace support, use a non-XML syntax, and offer limited extensibility. These limitations led to the development of alternative schema languages including XML Schema (XSD), RELAX NG, and Schematron. XML Schema provides strong data typing, namespace support, and uses XML syntax itself. RELAX NG offers a simpler, more flexible approach to schema definition. Despite these alternatives, DTDs remain in use for legacy systems and simpler applications where their lightweight nature is advantageous.
Comments (0)
No comments yet. Be the first to comment!