Skip to content

XML DTD

A well-formed XML document follows basic syntax rules. A valid XML document is well-formed AND conforms to a specific structure defined by a schema like a DTD or XSD.

DTD is the original mechanism for defining the legal building blocks of an XML document.

Note: While DTDs are foundational, XML Schema Definition (XSD) is the modern, more powerful, and generally preferred standard for XML validation.

A DTD defines the structure and legal elements and attributes of an XML document. You can use a DTD to validate that an XML document adheres to a predefined format.

An XML document references its DTD using the <!DOCTYPE> declaration, placed after the optional XML declaration and before the root element.

Example referencing an external DTD file named Note.dtd:

<?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>

The DTD itself (Note.dtd) contains declarations defining the structure.

Here’s an example of what the contents of Note.dtd might look like (or it could be embedded directly in the <!DOCTYPE> declaration using square brackets []):

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

Interpretation:

  • <!ELEMENT note (to, from, heading, body)>: Defines the root element note and specifies that it must contain exactly one to, from, heading, and body element, in that specific order.
  • <!ELEMENT to (#PCDATA)>: Defines the to element to contain Parsed Character Data (text that will be parsed by the parser). Similar definitions apply to from, heading, and body.

#PCDATA is essentially text content, possibly including markup to be parsed.

DTDs can also declare entities, which act as shortcuts for special characters or reusable pieces of text.

Example using an internal DTD subset to declare entities:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp "&#xA0;"> <!-- non-breaking space -->
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>&writer;&nbsp;&copyright;</footer>
</note>

Here, &writer;, &nbsp;, and &copyright; are replaced by their defined values during parsing.

Note: Predefined XML entities like &lt;, &gt;, &amp;, &quot;, &apos; do not need to be declared.

Why Use a DTD? (Historical Context and Modern Alternatives)

Section titled “Why Use a DTD? (Historical Context and Modern Alternatives)”

Historically, DTDs were used to:

  • Define a standard structure for data exchange between different groups or systems.
  • Verify that received XML data conformed to an expected format.

However, DTDs have significant limitations:

  • No support for namespaces.
  • Very limited data typing (mostly just text).
  • DTD syntax is not XML-based.
  • Less expressive power compared to XSD.

Because of these limitations, XML Schema Definition (XSD) is now the preferred W3C standard for defining XML structure and validation in most modern applications. XSD offers strong data typing, namespace support, and uses XML syntax.

You might still encounter DTDs in legacy systems or specific older standards.

To learn more about modern XML validation, study XML Schema (XSD).