|
|
 |

 |
|
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.
|
|
|
|
|
 |
|
|
Chapter 3: Creating DTDs continued
Combining Internal and External DTDs
XML documents can have an internal and an external DTD. To do this, you add the internal DTD declarations after specifying the location of the external DTD. As before, the internal declarations begin with the open bracket ([) and end with the closing bracket and the greater than sign (]>).
Listing 3-4 shows an example document with an internal DTD and a nonpublic external DTD. Note the reference to the external nonpublic DTD (pospec.dtd) as well as the internal DTD declarations.
Listing 3-4. An XML Document with an Internal and External DTD
<?xml version="1.0" standalone="no"?> <!DOCTYPE purchase_order SYSTEM "pospec.dtd"[ <!ELEMENT purchase_order (customer)> <!ELEMENT customer (account_id, name)> <!ELEMENT account_id (#PCDATA)> <!ELEMENT name (first, mi, last)> <!ELEMENT first (#PCDATA)> <!ELEMENT mi (#PCDATA)> <!ELEMENT last (#PCDATA)> ]><purchase_order> <customer> <account_id>10-487</account_id> <name> <first> William </first> <mi> R </mi> <last> Stanek </last> </name> </customer> </purchase_order>
If you decide to create documents with an internal and external DTD, you should ensure that the two DTDs are compatible. When determining compatibility, keep in mind these basic rules:
- Neither DTD can override the element or attribute declarations of the other. This also means that the DTDs can't contain the same element or attribute declarations.
- Entity declarations can be declared as external and can be redefined in an internal DTD. If there are conflicting entity declarations, the first declaration has precedence. Because internal DTDs are read first, internal declarations have precedence over identically named external references. However, when the external reference is read, its definition is still applied.
Adding Internal and External DTDs to Documents
To add an internal and external DTD to an XML document, follow these steps:
- Open your XML document for editing. In the XML declaration, add standalone="no" (or replace an existing value of yes with no).
- Type <!DOCTYPE root_name, where root_name is the name of the document's root element.
- Type PUBLIC or SYSTEM as appropriate. The PUBLIC keyword indicates a public, standard DTD. The SYSTEM keyword indicates a nonpublic, nonstan-dard DTD.
- If you're specifying a public external DTD, type the public ID of the external DTD between quotation marks, such as: "-//Stanek//PO Specification//EN".
- Type the URL for the public DTD between quotation marks, such as: "http://www.microsoft.com/pospec.dtd".
- Type [.
- Enter a few blank lines, in which you'll later enter your declarations as discussed in Chapters 4, 5, and 6.
- Type ]> to complete the DTD.
The result should look similar to the following example:
<?XML version="1.0" standalone="no"?> <!DOCTYPE purchase_order PUBLIC "-//Stanek//PO Specification//EN" "http://www.microsoft.com/pospec.dtd" [ ]>
Writing External DTD Files
DTD files are standard Unicode or ASCII text files that contain the definitions you're declaring externally. In the DTD file, you don't enter the DOCTYPE declaration or any formatting characters other than those required for the definitions being declared. Although the file can be named with any valid system name, it's better to name the file with the .dtd extension. The .dtd extension makes it easy to recognize that the file contains a DTD.
Listing 3-5 provides the contents of an external DTD file and shows how the file could be referenced in a conforming document.
Listing 3-5. An External DTD File with an Associated XML Document File
Filename: pospec.dtd
<!ELEMENT purchase_order (customer)> <!ELEMENT customer (account_id, name)> <!ELEMENT account_id (#PCDATA)> <!ELEMENT name (first, mi, last)> <!ELEMENT first (#PCDATA)> <!ELEMENT mi (#PCDATA)> <!ELEMENT last (#PCDATA)>
Filename: purchase.xml
<?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>
Although DTD files can contain blank lines and properly formatted comments, they shouldn't contain XML or DOCTYPE declarations. Additionally, XML elements aren't allowed inside a DTD. An example DTD file with properly formatted comments follows:
<! Purchase Order Specification V2.1 > <! Author: William Stanek > <! Last modified: 12/15/01 > <!ELEMENT purchase_order (customer)> <!ELEMENT customer (account_id, name)> <!ELEMENT account_id (#PCDATA)> <!ELEMENT name (first, mi, last)> <!ELEMENT first (#PCDATA)> <!ELEMENT mi (#PCDATA)> <!ELEMENT last (#PCDATA)>
Note that DTD comments use the same syntax as standard XML comments, beginning with <!-- and ending with -->.
Previous
| Table of Contents
| Next
Last Updated: January 17, 2002
|