As we have already alluded to several times throughout this tutorial, a
DTD can either be included as part of a well-formed XML document (standalone=yes)
or it can be referenced from an external source (standalone=no).
The benefits of using external DTDs is that they can more easily and efficiently be
shared by more than one XML document, or in fact, many organizations with the
need to standardize communications and data. You can write a DTD once
and have multiple documents reference it. Not only does this save
typing time, but it assures that as the DTD manager makes changes to the
central DTD, all documents that rely on the DTD are updated in one fell
swoop (Of course, DTD changes will not necessarily be backwards compatible,
so watch out!).
In order to reference an external DTD, you must change both the XML
declaration and the DOCTYPE declaration.
The XML Declaration must be changed to reflect the fact that the XML
document will not work on its own. That is, it will not be standalone.
<?xml version = "1.0"
encoding="UTF-8"
standalone = "no"?>
You will also need to change the DOCTYPE declaration to add the SYSTEM
attribute.
<!DOCTYPE ROOT_ELEMENT
SYSTEM "URL_OF_EXTERNAL_DTD">
such as....
<!DOCTYPE CONTACTS
SYSTEM "http://www.mydomain.com/dtds/contacts.dtd">
Note also, that the URL may be a relative or absolute file location such
as....
<!DOCTYPE CONTACTS
SYSTEM "contacts.dtd">
which specifies a dtd file in the same directory as the XML document that
references it. Or, similarly, you can reference the same document up
one directory and down one into the "dtds" directory.
<!DOCTYPE CONTACTS
SYSTEM "../dtds/contacts.dtd">
Using this method, you can simply cut out the DTD from your XML document
and paste it into the separate document called contacts.dtd. Thus, you
have one file with the DTD and one file with the well-formed XML
document.