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
- Self-Contained: Everything needed to validate the document exists within a single file.
- No Network Latency: XML parsers do not need to make additional file system or network requests to retrieve the schema rules.
- Limited Scope: The schema rules apply only to that specific XML document and cannot be shared.
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
- High Reusability: Multiple XML documents can reference the exact same DTD file.
- Centralized Maintenance: Updating the schema rules
in the single
.dtdfile automatically updates validation across all linked XML documents. - External Dependency: The XML parser must be able to locate and access the external file, which can introduce network overhead or failure points if the file is moved.
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
- Choose an Internal DTD when creating small, standalone XML files, testing schema prototypes, or when sending data across systems where external file dependencies might break.
- Choose an External DTD for enterprise environments, standard document formats, or large projects where multiple XML documents must adhere to identical structural standards.