Skip to content

DTD Intro

Introduction to Document Type Definition (DTD)

Section titled “Introduction to Document Type Definition (DTD)”

A Document Type Definition (DTD) defines the allowed structure, elements, and attributes for an XML document. It acts as a schema or blueprint.

Using a DTD ensures that an XML document adheres to a predefined format, making it ‘valid’ according to that definition.

A DTD can be declared directly within the XML document (internal DTD) or linked as an external file (external DTD).

Before diving into DTDs, you should have a basic understanding of:

  • Basic HTML structure
  • Fundamental XML syntax (elements, attributes, well-formedness)

If these concepts are new, consider reviewing introductory XML materials first.

Defining a structure for your XML documents provides several benefits:

  • Validation: Applications can automatically check if received XML data conforms to the expected structure, preventing errors.
  • Interoperability: When groups agree on a common DTD (or schema), it ensures consistency when exchanging data.
  • Documentation: A DTD serves as documentation for the XML format.
  • Development Assistance: Many XML editors and IDEs use DTDs/schemas to provide auto-completion and validation during development.

While DTDs were an early method, XML Schema Definition (XSD) is a more modern, powerful, and widely used alternative offering richer data types and namespace support. However, understanding DTDs provides foundational knowledge.

An internal DTD is declared within the XML file itself, enclosed in the <!DOCTYPE ... [...]> declaration.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Explanation of the DTD part:

  • <!DOCTYPE note ...>: Defines note as the root element of the document.
  • <!ELEMENT note (to, from, heading, body)>: Specifies that the note element must contain exactly one each of to, from, heading, and body elements, in that sequence.
  • <!ELEMENT to (#PCDATA)>: Defines the to element to contain Parsed Character Data (text).
  • <!ELEMENT from (#PCDATA)>, <!ELEMENT heading (#PCDATA)>, <!ELEMENT body (#PCDATA)>: Similarly define these elements to contain text.

An external DTD is defined in a separate .dtd file. The XML document then references this file using the <!DOCTYPE> declaration with a SYSTEM or PUBLIC identifier.

XML File (myNote.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

External DTD File (note.dtd):

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Using an external DTD is generally preferred for reusability, as the same DTD can be applied to multiple XML documents.