Internal DTD vs External DTD
Estimated study time: 12 minutes.
A DTD can live in one of two places: directly inside the XML document (internal), or in a separate file referenced by the document (external).
Internal DTD
Declared inside the document's own <!DOCTYPE> block, using square brackets.
<!DOCTYPE course [
<!ELEMENT course (title, duration)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT duration (#PCDATA)>
]>
<course>
<title>Data Analytics</title>
<duration>4 Months</duration>
</course>
Good for small, self-contained documents where the rules only apply to that one file.
External DTD
Stored in a separate .dtd file and referenced by URL or path.
<!DOCTYPE course SYSTEM "course.dtd">
<course>
<title>Data Analytics</title>
<duration>4 Months</duration>
</course>
<!-- course.dtd -->
<!ELEMENT course (title, duration)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT duration (#PCDATA)>
Choosing Between Them
- Internal — quick, self-contained, good for one-off or small documents.
- External — reusable across many documents, easier to maintain a single source of truth for shared structure rules.
💡 Tip: If multiple XML files need to follow the same rules, always use an external DTD — updating one file automatically enforces the change everywhere it's referenced.