Training
Certifications
Books
Special Offers
Community




 
XML Pocket Consultant
Author William R. Stanek
Pages 416
Disk N/A
Level All Levels
Published 01/16/2002
ISBN 9780735611832
Price $29.99
To see this book's discounted price, select a reseller below.
 

More Information

About the Book
Table of Contents
Sample Chapter
Index
Related Series
About the Author

Support: Book & CD

Rate this book
Barnes Noble Amazon Quantum Books

 


Chapter 3: Creating DTDs continued


Working with External DTDs

You 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 DTDs

Standard, 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"
     "/XML/1998/06/xmlspec-v20.dtd">

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:

  • - (Minus)  Indicates that the DTD isn't a recognized standard. A plus (+) here would have meant that the DTD is a recognized standard.
  • W3C  Specifies the owner of the DTD as the World Wide Web Consortium (W3C). This means that the W3C wrote and maintains the DTD. The owner can be a person or an organization.
  • DTD Specification V2.0  Sets a descriptive label for the DTD. The label can contain any standard characters except double slashes (//).
  • EN  A two-letter abbreviation for the language of the XML documents to which the DTD applies. In this case the language is U.S. English. A complete list of two-letter language abbreviations is specified in ISO 639-1.

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"?>
<!DOCTYPE purchase_order PUBLIC "-//Stanek//PO Specification//EN"
 "http://www.tvpress.com/pospec.dtd">
<purchase_order>
 <customer>
  <account_id>10-487</account_id>
  <name>
   <first> William </first>
   <mi> R </mi>
   <last> Stanek </last>
  </name>
 </customer>
</purchase_order>

Adding Public External DTDs to Documents

To add a public external DTD to an XML document, follow these steps:

  1. Open your XML document for editing. In the XML declaration, add standalone="no" (or replace an existing value of yes with no).
  2. Type <!DOCTYPE root_name, where root_name is the name of the document's root element.
  3. Type PUBLIC to indicate that the external DTD is publicly accessible. Be sure there's a space before and after the keyword.
  4. Type the public ID of the external DTD between quotation marks, such as: "-//Stanek//PO Specification//EN".
  5. Type the URL for the public DTD between quotation marks, such as: "http://www.microsoft.com/pospec.dtd".
  6. Type > to complete the declaration.

The result should look similar to the following:

<!DOCTYPE purchase_order PUBLIC "-//Stanek//PO Specification//EN"
 "http://www.microsoft.com/pospec.dtd">

Declaring Nonpublic External DTDs

DTDs 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 
 "http://www.microsoft.com/pospec.dtd">

In this example:

  • purchase_order  The designator for the root element, which is the name of the root of the XML tree.
  • SYSTEM  Identifies an external DTD that isn't public and generally is created by an organization or individual for their own purposes.
  • http://www.microsoft.com/pospec.dtd  Sets the URL for the DTD, which could be relative to a specific location or an absolute URL to a specific location on a remote server.

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"?>
<!DOCTYPE purchase_order SYSTEM "pospec.dtd">
<purchase_order>
 <customer>
  <account_id>10-487</account_id>
  <name>
   <first> William </first>
   <mi> R </mi>
   <last> Stanek </last>
  </name>
 </customer>
</purchase_order>

Adding Nonpublic DTDs to Documents

To add a nonpublic DTD to an XML document, follow these steps:

  1. Open your XML document for editing. In the XML declaration, add standalone="no" (or replace an existing value of yes with no).
  2. Type <!DOCTYPE root_name, where root_name is the name of the document's root element.
  3. Type SYSTEM to indicate that the external DTD is nonpublic and nonstandardized. Be sure there's a space before and after the keyword.
  4. Type the URL for the DTD between quotation marks, such as: "pospec.dtd".
  5. Type > to complete the declaration.

The result should look similar to the following:

<!DOCTYPE purchase_order SYSTEM "pospec.dtd">

Resolving Errors with Externally Referenced DTDs

The 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.


Previous   |  Table of Contents   |   Next



Last Updated: January 17, 2002
Top of Page