|
Chapter 3: Creating DTDs continued
Working with External DTDsYou specify external DTDs using a DOCTYPE assignment that contains a Uniform Resource Identifier (URI). The URI in the assignment identifies the location of the DTD. Because URIs are a superset of Uniform Resource Locators (URLs) and Uniform Resource Names (URNs), XML documents can reference both a URL and a URN.The DOCTYPE declaration must occur after the XML declaration but before the root element. Officially, the part of the XML document before the root element start tag is called the prolog. You can think of the prolog as a header, much like the header in HTML documents. As discussed previously in this chapter, there are two types of external DTDs: public and nonpublic. The sections that follow examine each type of external DTD.
Declaring Public External DTDsStandard, publicly accessible DTDs are specified using the keyword PUBLIC in the DOCTYPE declaration. A public DTD can have a public ID, officially referred to as a formal public identifier (FPI). The idea is that an XML parser could use the public ID to find the latest version of the DTD on a public server. In practice, however, most XML parsers rely on the public ID to locate and validate documents.The following document type declaration refers to the version 2.0 DTD specification for XML 1.0:
<!DOCTYPE spec PUBLIC "-//W3C//DTD Specification V2.0//EN" By examining the previous declaration, you can learn many things about how declarations are defined and used. The example declaration says that the root element is spec and then specifies information about the DTD's owner and location. The owner information is supplied first as a URN:
-//W3C//DTD Specification V2.0//EN The double slashes (//) separate categories of information regarding the DTD and its owner:
Unlike the URN, which allows the DTD to be located by a publicly identified name, the next section of the declaration refers to a static URL:
"/XML/1998/06/xmlspec-v20.dtd" In this example the URL is relative to a location on a specific server but could also have been an absolute URL that pointed to a specific location on a remote server, such as:
"http://www.w3.org/XML/1998/06/xmlspec-v20.dtd" The important thing to note about the URL is the DTD file name, which is xmlspec-v20.dtd. As with XML file names, the extension for DTDs doesn't have to be .dtd, as shown. However, the .dtd extension does make it easier for you and other developers to locate your DTDs. Listing 3-2 provides the source for a basic document with an external DTD that is public. As with an internal DTD definition, an external DTD definition declares the root element, which in this case is purchase_order. The document type declaration is in turn followed by the document's contents. Listing 3-2. An XML Document with a Public External DTD
<?xml version="1.0" standalone="no"?>
Adding Public External DTDs to DocumentsTo add a public external DTD to an XML document, follow these steps:
The result should look similar to the following:
<!DOCTYPE purchase_order PUBLIC "-//Stanek//PO Specification//EN"
Declaring Nonpublic External DTDsDTDs that organizations and individuals create for their own purposes are personal DTDs and are declared with the keyword SYSTEM rather than PUBLIC. With nonpublic DTDs, the standard declaration usually looks like this:
<!DOCTYPE purchase_order SYSTEM In this example:
Listing 3-3 provides the source for a basic document with an external DTD that isn't public. Again, the document type declaration is followed by contents of the document. Note that the DTD location reflects only a file name, which means the file is in the same directory as the associated XML document. Listing 3-3. An XML Document with a Nonpublic External DTD
<?xml version="1.0" standalone="no"?>
Adding Nonpublic DTDs to DocumentsTo add a nonpublic DTD to an XML document, follow these steps:
The result should look similar to the following:
<!DOCTYPE purchase_order SYSTEM "pospec.dtd">
Resolving Errors with Externally Referenced DTDsThe XML parser processing a document must be able to locate external DTDs using the URI you've provided. If the parser is unable to locate the DTD, the error you'll see usually specifies that the system can't locate the resource specified or that there was an error processing the resource DTD.If this occurs, check the accuracy and syntax of the URL you're using. Keep in mind that URLs in the form dir_name/file.dtd are located relative to the current directory but URLs in the form /dir_name/file.dtd are located relative to the root directory for the server or current file system. Further, if only a filename is specified as the URL, the DTD is expected to be in the same directory as the associated XML document.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||