XML External DTD
An external DTD stores validation rules in a separate file with a .dtd extension. Multiple XML files reference the same DTD file. When you change the rules, you update one DTD file — and every XML file that references it immediately benefits from the change.
Think of a company's employee handbook. Every department follows the same handbook. When HR updates a policy, they update the handbook once. All departments get the new rules automatically. An external DTD works exactly this way.
External DTD File Structure
An external DTD file contains only DTD declarations — no XML prolog, no root element. It is plain DTD syntax saved with a .dtd extension.
File: product.dtd
<!ELEMENT catalog (product+)> <!ELEMENT product (name, sku, price, stock?)> <!ATTLIST product id ID #REQUIRED category CDATA #IMPLIED > <!ELEMENT name (#PCDATA)> <!ELEMENT sku (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT stock (#PCDATA)> <!ENTITY currency "INR">
Referencing an External DTD from XML
An XML file references an external DTD using SYSTEM (for local files) or PUBLIC (for publicly registered identifiers).
SYSTEM Reference (local file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "product.dtd">
<catalog>
<product id="P001" category="stationery">
<name>Ruled Notebook</name>
<sku>ST-NB-001</sku>
<price>45</price>
<stock>200</stock>
</product>
<product id="P002">
<name>Blue Pen Pack</name>
<sku>ST-PN-002</sku>
<price>30</price>
</product>
</catalog>
PUBLIC Reference (shared/published DTD)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
PUBLIC DTDs have a formal name (the string starting with - or +) plus a URL as a fallback. XHTML, SVG, and RSS all use public DTDs published by standards organizations.
How the Parser Loads an External DTD
XML File Processing Steps:
1. Parser reads <?xml version="1.0"?>
|
v
2. Parser finds <!DOCTYPE catalog SYSTEM "product.dtd">
|
v
3. Parser opens and reads product.dtd
|
v
4. Parser builds validation rules from the DTD
|
v
5. Parser reads the XML content
|
v
6. Parser checks each element and attribute
against the loaded DTD rules
|
v
7. Valid ✓ or Invalid ✗ — report generated
Multiple XML Files Sharing One DTD
This is the most powerful use of external DTD. A chain of electronics stores maintains daily stock reports. Each store sends an XML file. All files reference the same DTD.
Shared DTD: store-report.dtd
<!ELEMENT report (storeName, date, item+)> <!ELEMENT storeName (#PCDATA)> <!ELEMENT date (#PCDATA)> <!ELEMENT item (itemName, quantity, value)> <!ATTLIST item code ID #REQUIRED unit CDATA "units" > <!ELEMENT itemName (#PCDATA)> <!ELEMENT quantity (#PCDATA)> <!ELEMENT value (#PCDATA)>
Store A — store-a.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE report SYSTEM "store-report.dtd">
<report>
<storeName>MG Road Branch</storeName>
<date>2024-01-15</date>
<item code="I001">
<itemName>Laptop 15"</itemName>
<quantity>12</quantity>
<value>720000</value>
</item>
</report>
Store B — store-b.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE report SYSTEM "store-report.dtd">
<report>
<storeName>Koramangala Branch</storeName>
<date>2024-01-15</date>
<item code="I002">
<itemName>Wireless Mouse</itemName>
<quantity>45</quantity>
<value>40500</value>
</item>
</report>
Both stores use the same rules. If headquarters needs to add a new required field like taxCode, they update store-report.dtd once, and both store files instantly validate against the new rule.
SYSTEM Paths — Absolute vs Relative
| Path Type | Example | Use When |
|---|---|---|
| Relative path | "product.dtd" | DTD is in the same folder as the XML file |
| Relative path | "../dtd/product.dtd" | DTD is in a parent folder |
| Absolute URL | "http://example.com/dtd/product.dtd" | DTD is hosted on a server |
| Absolute local | "file:///C:/schemas/product.dtd" | Full path on local machine |
Combining Internal and External DTD
An XML file can use both an external DTD file and add extra internal rules. The internal rules extend or override the external DTD for that specific file.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE catalog SYSTEM "product.dtd" [ <!ENTITY siteURL "https://www.mystore.com"> <!ELEMENT extraNote (#PCDATA)> ]> <catalog> ... </catalog>
External DTD vs Internal DTD
| Feature | Internal DTD | External DTD |
|---|---|---|
| Location | Inside the XML file | Separate .dtd file |
| Reusability | No — one file only | Yes — shared by many files |
| Maintenance | Change each file separately | Change one DTD file |
| File distribution | One file to send | Both XML and DTD files needed |
| Best for | Standalone files, learning | Enterprise systems, shared schemas |
Key Points to Remember
- An external DTD is a separate .dtd file containing validation rules.
- Reference it with <!DOCTYPE rootName SYSTEM "file.dtd"> or PUBLIC identifier.
- Multiple XML files reference one DTD — update rules in one place.
- SYSTEM points to a local path or URL. PUBLIC points to a registered identifier.
- External and internal DTD can combine — internal rules extend the external ones.
- External DTD is the preferred choice for enterprise and multi-file XML systems.
