DTD - external DTD declaration

An  means the DTD rules are stored in a separate file (usually .dtd) and the XML file just points to it.

Example

note.xml (the XML file)

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
  <to>Alice</to>
  <from>Bob</from>
  <heading>Reminder</heading>
  <body>Don't forget our meeting at 10 AM tomorrow.</body>
</note>

note.dtd (the external DTD file)

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

Key points:

  • In the XML file,

    • <!DOCTYPE note SYSTEM "note.dtd"> tells the parser to load rules from note.dtd.

    • note is the root element that the DTD validates.

  • The XML and DTD are kept separate, which is good if many XML files share the same structure.

  • If you change the DTD file, all linked XML files follow the new rules automatically.