Document type definition
A Document Type Definition (DTD for short) is a declaration in an SGML or XML document that specifies constraints on the structure of an SGML or XML document. It is written in a discrete ascii-text file.
Defining a DTD specifies the syntax of a language such as HTML, XHTML or XSL.
DTDs are usually employed to determine the structure of an XML document. A DTD will typically describe each allowable element within the XML document, the possible attributes and (optionally) the allowed attribute values for each element. Further, it will describe the nesting and occurrences of elements, and also make attributions to any external attributes. The preponderance of a DTD is composed of ELEMENT definitions and ATTRIBUTE definitions.
An example of a very simple DTD to describe a person is given below:
<!ELEMENT people_list (character)>
<!ELEMENT character (name, birthdate, gender, socialsecuritynumber)>
An example of an XML file which makes use of this DTD follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE windows SYSTEM "example.dtd">
<people_list>
<character name="Fred Bloggs">
</character>
<character birthdate="27/11/2008">
</character>
<character gender="Male">
</character>
</people_list>
You should note that the DTD given above does not require that any element is mandatory. (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 an ascii-text file named example.dtd and the xml file to a differently-named ascii-text file, and opening the xml file with the browser. NB you should save the files in the same directory.
See Also: