Document Type Definition

A Document Type Definition (DTD for short) is a set of declarations that conform to a particular markup syntax and that describe a class, or "type", of SGML or XML documents, in terms of constraints on the structure of those documents.

A DTD specifies, in effect, the syntax of an "application" of SGML or XML, such the derivative language HTML or XHTML. This syntax is usually a less general form of the syntax of SGML or XML.

In a DTD, the structure of a class of documents is described 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 value(s).

A DTD may also declare default attribute values, named entities and their replacement text, and other constructs that are not always required to establish document structure, but that may affect the interpretation of some documents.

Contents

Associating DTDs with documents

A DTD is associated with a particular document via a Document Type Declaration, which is a bit of markup that appears near the start of the associated document. The declaration establishes that the document is an instance of the type defined by the referenced DTD.

The declarations in a DTD are divided into an internal subset and an external subset. The declarations in the internal subset are embedded in the Document Type Declaration 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.

Examples

Here is an example of a Document Type Declaration containing 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">

Here is an example of a Document Type Declaration that encapsulates an internal subset consisting of a single entity declaration:

<!DOCTYPE foo [ <!ENTITY greeting "hello"> ]>

All HTML 4.01 documents are expected to conform to one of three SGML DTDs. The public identifiers of these DTDs are constant and are as follows:

The system identifiers of these DTDs, if present in the Document Type Declaration, will be URI references. System identifiers can vary, but are expected to point to a specific set of declarations in a resolvable location. SGML allows for public identifiers to be mapped to system identifiers in catalogs that are optionally made available to the URI resolvers used by document parsing software.

XML DTDs and schema validation

The XML DTD syntax is one of several XML schema languages. A common misunderstanding is that non-validating XML parsers are not required to read DTDs, when in fact, the DTD must still be scanned for correct syntax as well as declarations of entities and default attributes.

Differences between SGML and DTD syntax

The syntax of SGML and XML DTDs is very similar, but not identical.

  • The SGML declaration for HTML 4.01 (http://www.w3.org/TR/REC-html40/HTML4.decl), for example, allows its DTD (http://www.w3.org/TR/html4/sgml/dtd.html) to specify whether elements require start and end tags, which would be impossible in an XML DTD. Consider the following element declaration for HTML 4.01:
    <!ELEMENT BR - O EMPTY -- forced line break -->
    The - after the element name "BR" means a start tag, <BR>, is required and the O after that makes the end tag, </BR> optional (in fact, the W3C recommendation (http://www.w3.org/TR/REC-html40/struct/text.html#edef-BR) forbids the end tag). On the other hand, XML languages share a common SGML declaration (http://www.w3.org/TR/NOTE-sgml-xml-971215), one that simplifies the DTD syntax but disallows any tag omission (XML itself also prohibits comments within the declaration such as -- forced line break --). Thus, the XHTML 1.0 specification (http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-Strict) which specifies an XML-based version of HTML, only allows for
    <!ELEMENT br EMPTY>
    and the element must be written as either <br></br> or in a special shortened format as <br />. In addition, XML element tags are case-sensitive, so the HTML BR element must be written in lowercase in XHTML (http://www.w3.org/TR/xhtml1/#h-4.2) as defined above (br).
  • Element declarations in XML cannot exclude other elements. For example, in HTML,
    <!ELEMENT FORM - - (%flow;)* -(FORM) -- interactive form -->
    defines a FORM element that includes certain elements (with an SGML entity) but, due to the -(FORM) part, cannot include other FORMs. In XHTML the FORM is thus defined as
    <!ELEMENT form %form.content;>
    which simply includes certain elements.

Template:Section-stub

XML DTD Example

An example of a very simple XML DTD to describe a list of persons is given below:

<!ELEMENT people_list (person*)>
<!ELEMENT person (name, birthdate?, gender?, socialsecuritynumber?)>
<!ELEMENT name (#PCDATA) >
<!ELEMENT birthdate (#PCDATA) >
<!ELEMENT gender (#PCDATA) >
<!ELEMENT socialsecuritynumber (#PCDATA) >

Taking this line by line, it says:

  1. A "people_list" element contains any number of "person" elements. The "*" denotes there can be 0 or many "person" elements within the "people_list" element.
  2. A "person" element contains the elements "name", "birthdate", "gender" and "socialsecuritynumber". The "?" indicates that an element is optional. The "name" element does not have a "?", so a "person" element must contain a "name" element.
  3. A "name" element contains data.
  4. A "birthdate" element contains data.
  5. A "gender" element contains data.
  6. A "socialsecuritynumber" element contains data.

An example of an XML file which makes use of this DTD follows. It assumes the DTD is identifiable by the relative URI reference "example.dtd":

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE people_list SYSTEM "example.dtd">
 <people_list>
  <person>
    <name>Fred Bloggs</name>
    <birthdate>27/11/2008</birthdate>
    <gender>Male</gender>
  </person>
</people_list>

The DTD given above requires a name element within every person element; the people_list element is also mandatory, but the rest are optional.

It is possible to render this in an XML-enabled browser (such as IE5 or Mozilla) by pasting and saving the DTD component above to a text file named example.dtd and the XML file to a differently-named text file, and opening the XML file with the browser. The files should both be saved in the same directory. However, many browsers do not check that an XML document conforms to the rules in the DTD; they are only required to check that the DTD is syntactically correct. For security reasons, they may also choose not to read the external DTD.

DTD criticisms and alternatives

While DTD support in XML tools is widespread due to its inclusion in the XML 1.0 standard, it is seen as limited for the following reasons:

  • No support for newer features of XML — most importantly, namespaces.
  • Lack of expressivity. Certain formal aspects of an XML document cannot be captured in a DTD.
  • Custom non-XML syntax to describe the schema, inherited from SGML.

Three newer XML schema languages that are much more powerful are increasingly favored over DTDs:

See also

External links

es:DTD eo:Dokumento-Speco-Difino fr:Définition de type de document nl:Document Type Definitie ja:Document Type Definition pl:DTD ru:DTD sv:DTD zh:文件类型描述

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools