XML Internal DTD

An internal DTD embeds the validation rules directly inside the XML file itself. You do not need a separate file. Everything — the structure rules and the actual data — lives in one document. This makes the XML file completely self-contained.

Imagine a contract that includes its own list of legal definitions at the top. Anyone reading the contract finds both the definitions and the terms in one place. Internal DTD works the same way.

Internal DTD Syntax

The internal DTD sits between the XML prolog and the root element. It uses a DOCTYPE declaration with square brackets containing all the DTD rules.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root-element-name [
  <!-- DTD rules go here -->
  <!ELEMENT ...>
  <!ATTLIST ...>
  <!ENTITY ...>
]>
<root-element-name>
  <!-- XML content goes here -->
</root-element-name>

Structure Breakdown

<?xml version="1.0"?>       ← XML prolog
<!DOCTYPE library [          ← DOCTYPE + root element name + open bracket
  <!ELEMENT library ...>    ← element rules
  <!ELEMENT book ...>
  <!ATTLIST book ...>        ← attribute rules
  <!ENTITY ...>              ← entity shortcuts
]>                           ← close bracket ends DTD
<library>                   ← XML content begins
  ...
</library>

Example 1: Simple Contact Book

A personal contact book where each contact must have a name and phone number, and optionally an email:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE contacts [
  <!ELEMENT contacts (contact+)>
  <!ELEMENT contact (name, phone, email?)>
  <!ATTLIST contact
    type (personal | business | emergency) "personal"
  >
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT phone (#PCDATA)>
  <!ELEMENT email (#PCDATA)>
]>
<contacts>
  <contact type="business">
    <name>Vikram Patel</name>
    <phone>+91-9876543210</phone>
    <email>vikram@company.com</email>
  </contact>
  <contact type="personal">
    <name>Lakshmi Reddy</name>
    <phone>+91-9123456789</phone>
  </contact>
</contacts>

Example 2: School Exam Results

Exam results for a school, where each student must have a roll number attribute and at least one subject score:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE results [
  <!ELEMENT results (student+)>
  <!ELEMENT student (name, score+)>
  <!ATTLIST student
    rollNo ID       #REQUIRED
    grade  CDATA    #IMPLIED
  >
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT score (#PCDATA)>
  <!ATTLIST score
    subject CDATA   #REQUIRED
    maxMarks CDATA  "100"
  >
]>
<results>
  <student rollNo="R001" grade="A">
    <name>Kavya Sharma</name>
    <score subject="Mathematics">95</score>
    <score subject="Science">88</score>
    <score subject="English">91</score>
  </student>
  <student rollNo="R002">
    <name>Rajan Nair</name>
    <score subject="Mathematics">72</score>
    <score subject="Science">80</score>
    <score subject="English">65</score>
  </student>
</results>

Validation Flow with Internal DTD

Parser reads XML file
         |
         v
Finds <?xml version="1.0"?>   → notes XML version
         |
         v
Finds <!DOCTYPE results [...]> → loads internal DTD rules
         |
         v
Reads <results> root element
         |
         v
Checks: is "results" declared in DTD?
  No  → Validation error
  Yes → Continue
         |
         v
Checks each child element against DTD rules
         |
         v
Checks all required attributes are present
         |
         v
Document is VALID ✓

When to Use Internal DTD

  • The XML file is standalone and not part of a larger system.
  • You want quick validation without managing separate files.
  • You are learning or testing DTD concepts.
  • The DTD rules apply only to this single file, not shared with others.

Advantages of Internal DTD

  • Self-contained: One file holds everything — rules and data together.
  • No external dependency: The parser does not need to fetch a separate file.
  • Easy to distribute: Send one file and the recipient gets rules and data together.

Disadvantages of Internal DTD

  • Duplication: If 50 XML files share the same structure, you copy the DTD into all 50 files. A change in the rules requires editing all 50 files.
  • Larger file size: The DTD rules add to the file size.
  • Maintenance burden: Keeping rules consistent across many files is difficult.

When multiple XML files share the same structure, use an external DTD instead. Store the rules in one .dtd file and reference it from each XML file.

Common Errors with Internal DTD

ErrorCauseFix
Element not declaredUsed an element not in the DTDAdd <!ELEMENT> declaration
Wrong child orderChildren appear out of sequenceMatch the declared sequence
Missing required attributeAttribute marked #REQUIRED is absentAdd the attribute to the element
Invalid attribute valueValue not in enumeration listUse only declared values

Key Points to Remember

  • An internal DTD lives inside the XML file between the prolog and the root element.
  • The DOCTYPE declaration with square brackets holds all internal DTD rules.
  • Internal DTD makes the file self-contained and easy to distribute.
  • Duplicating internal DTDs across many files creates maintenance problems.
  • Use external DTDs when multiple XML files share the same structure.
  • Internal DTD is great for learning, testing, and standalone XML files.

Leave a Comment

Your email address will not be published. Required fields are marked *