Declaring DTDs
The World Wide Web Consortium (W3C) XML 1.0 specification specifies the use of document type definitions (DTDs) with XML documents. Although a document must be well formed in order to be fully and successfully parsed, not all XML parsers validate XML against a DTD or schema.
To use a DTD with an XML document, you need to first declare it in the document type declaration statement. To do so, you can use either of the following methods, separately or in combination:
- Insert the markup declarations that make up the DTD as part of the document type declaration.
- Declare the location of a file through a URI reference that points to an external file, typically a text file named using the .dtd file extension.
For more information about declaring DTDs, see DOCTYPE declaration.
Declaring an External DTD for System Use
To reference a personal external DTD, you need to provide a Web URL for the location of the DTD. For example, the following declares a document type called "catalog" with the DTD in a separate file (MyCatalog.dtd) published on an intranet server (MyServer).
<?xml version="1.0" standalone="no"?> <!DOCTYPE catalog SYSTEM "http://MyServer/MyCatalog.dtd">
While the DTD might be available and downloaded to others located on the same local network, the SYSTEM
keyword helps make it clear to the XML parser that the URL is the only method to use to locate the DTD. In this example, if the Web service on MyServer is offline, the DTD will not be found and the document cannot be validated when parsed.
If you are using or providing your DTD as a separate downloadable file that will be copied to the same local folder with your XML document, a simpler way to write the URI reference is just to include the file name, like this:
<!DOCTYPE catalog SYSTEM "MyCatalog.dtd">
Optionally, you can include path information to help Windows locate the external DTD file on either a local drive or a UNC file share, as in the following examples:
<!DOCTYPE catalog SYSTEM "C:\temp\MyCatalog.dtd"> <!DOCTYPE catalog SYSTEM "\\MyServer\MyShare\MyCatalog.dtd">
Naming a Public External DTD
If your DTD file is for use with XML documents that are published openly on the Internet, using a standard public naming convention will allow others to find and locate the file. To reference a public external DTD, you typically will use the PUBLIC
keyword in combination with a public identifier and a URI reference, as shown in the following example:
<!DOCTYPE rootElementPUBLIC "PublicIdentifier"
"URIreference"
>
This allows an XML application to first attempt to look up the public identifier and resolve it to the correct DTD to use for validating the document. The naming convention widely used for the public identifier is called a formal public identifier (FPI) and consists of the following fields:
- Type
- Owner
- Label
- Language
Type
The type is a "+" or "-" sign. The "+" sign indicates that the file is approved by a standards body such as the International Standards Organization (ISO). The "-" sign indicates the file is not a recognized standard. Type is the first character in the example below.
+//Owner Name//DTD Description_of_DTD//EN//
Owner
The owner identifies the person or organization that wrote and maintains the DTD. Owner is highlighted in the example below.
+//Owner Name
//DTD Description_of_DTD//EN//
Label
The label gives a description of the DTD. It begins with a space after the characters "DTD". Label is highlighted in the example below.
+//Owner Name//DTD Description_of_DTD
//EN//
Language
The language is a two-character abbreviation of the language of the XML document to which the DTD applies. The letters EN are used to specify the English language. Language is highlighted in the example below.
+//Owner Name//DTD Description_of_DTD//EN
//
Declaring a Public External DTD
To declare a public external DTD, you use the standard naming convention for the public identifier that names and refers to the DTD. As a backup, a static URL can be placed after the public identifying name and used if the public name fails to resolve.
The declaration in the XML document will look similar to the following:
<?xml version="1.0" standalone="no"
?>
<!DOCTYPE root element SYSTEM "+//Owner Name//DTD Description_of_DTD//EN//" " http://MyBackupServer/My.dtd