What is DTD in XML?

Estimated study time: 14 minutes.

A DTD (Document Type Definition) defines the legal building blocks of an XML document — which elements can appear, in what order, and what attributes they may carry. It's essentially a contract that a document must follow to be considered valid, not just well-formed.

Well-Formed vs Valid

A document is well-formed if it follows XML's basic syntax rules. It's valid if it also conforms to a DTD (or schema) that defines its expected structure. A document can be well-formed without being valid, but not the other way around.

A Simple DTD

<!DOCTYPE course [
  <!ELEMENT course (title, duration)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT duration (#PCDATA)>
]>
<course>
  <title>Data Analytics</title>
  <duration>4 Months</duration>
</course>

This DTD states that a course element must contain exactly a title followed by a duration, and both hold plain text (#PCDATA).

Why Use a DTD?

  • Guarantees documents from different sources follow the same structure.
  • Catches structural errors before data reaches downstream systems.
  • Documents the expected shape of the data for other developers.
💡 Tip: DTDs are still common in legacy systems, but for new projects consider XML Schema (XSD), which offers stronger data-typing support.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →