External DTDs


It is not considered best practice to build DTDs within your XML documents. Instead, it is best to reference an external DTD from the XML file. You want this level of abstraction between the data and the data definition. Tying them together makes future changes more difficult.

An internal DTD is really effective only if you have a single instance of this XML file and you don't plan on having any additional copies of the file. If you create more than a single instance of the XML structure, you must to go into each and every instance to make any change. If a DTD is referenced from your XML file, you can make any change to the document in a single place to have it instantly reflected across all the XML files that use that particular DTD. This level of abstraction is simply more powerful.

Creating an external DTD is simple. In the XML file from Listing 5-5, you simply make a single line reference to the DTD you want to use as your document definition. This is presented in Listing 5-7.

Listing 5-7: Building an XML document with an external DTD

image from book
      <?xml version="1.0" encoding="UTF-8" ?>      <!DOCTYPE Process SYSTEM "ProcessOrder.dtd">      <Process>         <Name>Bill Evjen</Name>         <Address>123 Main Street</Address>         <City>Saint Charles</City>         <State>Missouri</State>         <Country>USA</Country>         <Order>            <Item>52-inch Plasma</Item>            <Quantity>1</Quantity>         </Order>      </Process> 
image from book

In this case, Listing 5-7 shows a reference to the ProcessOrder.dtd document by using the <!DOCTYPE> declaration within the XML file.

      <!DOCTYPE Process SYSTEM "ProcessOrder.dtd"> 

This declaration comes after the <?xml> declaration and directly prior to the opening of the root element <Process>. By using “ProcessOrder.dtd” within this <!DOCTYPE> declaration, you are stating that the DTD file can be found in the same spot as the XML file itself. If this is not the case, you can assign a different path for the DTD file.

One option is to locate the DTD on the Internet by using a complete URL, as presented here:

      <!DOCTYPE Process SYSTEM "http://www.wrox.com/files/dtd/ProcessOrder.dtd"> 

This code is directing the parser to look for the DTD using the absolute path to a remote location on the Internet. You can also direct the parser to look for the DTD using an absolute path located within a network as follows:

      <!DOCTYPE Process SYSTEM "C:\Wrox\Files\DTD\ProcessOrder.dtd"> 

Because the location utilized is a relative URL, you can also provide a relative path as follows:

      <!DOCTYPE Process SYSTEM "../ProcessOrder.dtd"> 

Or

      <!DOCTYPE Process SYSTEM "Files/ProcessOrder.dtd"> 

After you have defined where to find the DTD file in your XML file, the next step is to actually create the DTD file. Listing 5-8 shows the ProcessOrder.dtd file.

Listing 5-8: The ProcessOrder.dtd

image from book
            <?xml version="1.0" encoding="UTF-8"?>      <!ELEMENT Address (#PCDATA)>      <!ELEMENT City (#PCDATA)>      <!ELEMENT Country (#PCDATA)>      <!ELEMENT Item (#PCDATA)>      <!ELEMENT Name (#PCDATA)>      <!ELEMENT Order (Item, Quantity)>      <!ELEMENT Process (Name, Address, City, State, Country, Order)>      <!ELEMENT Quantity (#PCDATA)>      <!ELEMENT State (#PCDATA)> 
image from book

Save this text file with a .dtd file extension, and your DTD file is ready for action.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net