Internal vs External XML DTD: Key Differences

This article provides a direct comparison between internal and external XML Document Type Definitions (DTDs). A DTD defines the legal building blocks, elements, and attributes of an XML document to ensure structural validity. Understanding whether to declare a DTD internally within an XML file or externally as a standalone file depends on factors such as reusability, maintenance, syntax, and performance.

What Is an Internal XML DTD?

An internal DTD is defined directly inside the XML document itself. The declarations are wrapped inside square brackets [...] within the <!DOCTYPE> declaration before the root element begins.

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

Characteristics of Internal DTDs


What Is an External XML DTD?

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

The DTD file (note.dtd):

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

The XML file referencing the 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>

Characteristics of External DTDs


Core Differences at a Glance

Feature Internal DTD External DTD
Location Embedded inside the XML file Stored in an external .dtd file
Reusability None; single document only High; shared across multiple documents
Maintenance High effort (must update each XML file individually) Low effort (update one .dtd file for all documents)
Parser Overhead Fast; no external I/O required Requires additional file read or HTTP request
Portability Completely self-contained in one file Requires maintaining paths to linked files

When to Use Which