← Back to Curriculum

XML DTD (Document Type Definition)

📚 Lesson 4 of 15 ⏱ 40 min

XML DTD (Document Type Definition)

40 min

DTD (Document Type Definition) defines the structure and rules for XML documents, specifying allowed elements, attributes, and their relationships. DTDs enable validation—checking that XML documents conform to a defined structure. DTDs are the original XML schema language, simpler than XSD but less powerful. Understanding DTDs enables you to define and validate XML document structures. DTDs are still used, though XSD is more common in modern applications.

DTDs can be internal (within the XML document) or external (in a separate file), providing flexibility in how schemas are defined and shared. Internal DTDs are defined in the DOCTYPE declaration within the XML document. External DTDs are in separate files, referenced by the DOCTYPE declaration. Internal DTDs are convenient for standalone documents. External DTDs enable schema reuse across multiple documents. Understanding DTD placement enables you to choose appropriate approaches.

DTDs specify element types (what elements are allowed), attributes (what attributes elements can have), and content models (what content elements can contain). Element declarations use `<!ELEMENT name content>`. Attribute declarations use `<!ATTLIST element attribute type default>`. Content models specify allowed child elements and their order/cardinality. Understanding DTD syntax enables you to define document structures. DTDs use a compact but less intuitive syntax than XSD.

Content models in DTDs specify element content using operators: `,` (sequence), `|` (choice), `?` (optional), `*` (zero or more), `+` (one or more), and `#PCDATA` (text content). Sequences require elements in order. Choices allow one of several elements. Quantifiers specify how many times elements can appear. Understanding content models enables you to define complex structures. DTD content models are less expressive than XSD but sufficient for many cases.

DTD attribute types include CDATA (character data), ID/IDREF (unique identifiers and references), enumerated types (list of allowed values), and NMTOKEN (name token). ID attributes must be unique within a document. IDREF attributes reference ID values. Enumerated types restrict values to a list. Understanding attribute types enables you to define appropriate constraints. DTD attribute types provide basic validation capabilities.

Best practices include using external DTDs for reusable schemas, validating documents against DTDs, understanding DTD limitations (less expressive than XSD), and considering XSD for more complex requirements. Understanding DTDs enables you to define and validate XML structures. DTDs are simpler than XSD but less powerful—choose based on your needs. DTDs remain useful for simple validation requirements.

Key Concepts

  • DTD defines the structure and rules for XML documents.
  • DTDs can be internal (within XML) or external (separate file).
  • DTDs specify element types, attributes, and content models.
  • Content models use operators (,, |, ?, *, +) to define structure.
  • DTD attribute types include CDATA, ID, IDREF, and enumerated types.

Learning Objectives

Master

  • Creating DTDs to define XML document structure
  • Using internal and external DTDs
  • Defining element types and content models
  • Specifying attributes and their types

Develop

  • Understanding XML schema languages
  • Designing document structures
  • Appreciating DTD's role in XML validation

Tips

  • Use external DTDs for schemas shared across multiple documents.
  • Validate XML documents against DTDs to ensure correctness.
  • Understand DTD limitations—consider XSD for complex requirements.
  • Use meaningful element and attribute names in DTDs.

Common Pitfalls

  • Not validating against DTDs, allowing invalid structures.
  • Using DTDs for complex requirements that need XSD.
  • Not understanding content model operators, creating incorrect schemas.
  • Forgetting that DTDs are less expressive than XSD.

Summary

  • DTD defines the structure and rules for XML documents.
  • DTDs can be internal or external.
  • DTDs specify elements, attributes, and content models.
  • Understanding DTDs enables defining and validating XML structures.
  • DTDs are simpler than XSD but less powerful.

Exercise

Create an XML document with an internal DTD definition.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore [
  <!ELEMENT bookstore (book+)>
  <!ELEMENT book (title, author, price, category?)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT price (#PCDATA)>
  <!ELEMENT category (#PCDATA)>
  <!ATTLIST book id ID #REQUIRED>
  <!ATTLIST book language (en|es|fr) "en">
]>

<bookstore>
  <book id="b1" language="en">
    <title>XML Programming</title>
    <author>John Doe</author>
    <price>29.99</price>
    <category>Programming</category>
  </book>
  <book id="b2" language="en">
    <title>Web Design</title>
    <author>Jane Smith</author>
    <price>24.99</price>
  </book>
</bookstore>

Code Editor

Output