Internal DTDs


Before you begin building DTD documents, you should understand that a DTD can be defined in several places. A DTD is a set of text elements that follows a specific syntax. This text can be stored in the XML file that it defines, or it can be held in a separate file. If it is held within the same file as the XML it defines, then it is considered an inline DTD. If it is kept in a separate file, it is considered an external DTD and has a .dtd file extension.

To show an example of utilizing an internal DTD, suppose you are working with the XML document presented in Listing 5-5.

Listing 5-5: An XML document that needs a DTD

image from book
      <?xml version="1.0" encoding="UTF-8" ?>      <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

To place a DTD document within this XML document, you place the DTD definition directly after the <?xml> declaration as illustrated in Listing 5-6.

Listing 5-6: Providing the XML document an internal DTD

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <!DOCTYPE Process [      <!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)>      ]>      <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

At this point, I won't review the meaning of the DTD declaration. Note that it is possible to declare DTDs internally within the XML document. Many in the industry, however, would say this is an improper way to declare your DTDs. They would tell you it is better to provide some level of abstraction and declare your DTDs externally. Others prefer that the DTD that validates the XML document is encased within the document itself-eliminating the need to deal with two files.

Listing 5-6 shows that when the DTD is declared internally, it is presented immediately following the <?xml> declaration and right before the XML document's root element <Process>.

If you take the XML file from Listing 5-6 and view the file in Microsoft's Internet Explorer, you see the results illustrated in Figure 5-1.

image from book
Figure 5-1

From this image, you can see that the DTD is not fully presented, but instead IE instructs you to go to the source file to see the complete DTD. If you look at the same file through Mozilla's Firefox, you see that Firefox doesn't do anything to present the associated DTD document.

Next, this chapter explains how to create the same document as an external DTD.




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