Using Both Internal and External DTDs

In fact, you can use both internal and external DTDs at the same time by using these forms of the <!DOCTYPE> element: <!DOCTYPE rootname SYSTEM URL [ DTD ]> for private external DTDs, and <!DOCTYPE rootname PUBLIC FPI URL [ DTD ]> for public external DTDs. In this case, the external DTD is specified by URL and the internal one by DTD .

Here's an example in which I've removed the <PRODUCT> element from the external DTD ch03_10.dtd:

 <!ELEMENT DOCUMENT (CUSTOMER)*>  <!ELEMENT CUSTOMER (NAME,DATE,ORDERS)> <!ELEMENT NAME (LAST_NAME,FIRST_NAME)> <!ELEMENT LAST_NAME (#PCDATA)> <!ELEMENT FIRST_NAME (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT ORDERS (ITEM)*> <!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)> 

Now I'll specify that I want to use this external DTD in the document's <!DOCTYPE> element and then also add square brackets, [ and ] , to enclose an internal DTD:

 <?xml version = "1.0" standalone="no"?>  <!DOCTYPE DOCUMENT SYSTEM "ch03_10.dtd" [   .   .   .   ]>  <DOCUMENT>     <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>                     <PRODUCT_ID>                         198348209                     </PRODUCT_ID>                 </PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             .             .             .             <ITEM>                 <PRODUCT>                     <PRODUCT_ID>                         198348206                     </PRODUCT_ID>                 </PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER> </DOCUMENT> 

Next, I add the declaration of the <PRODUCT> element to the internal part of the DTD, like this:

 <?xml version = "1.0" standalone="no"?>  <!DOCTYPE DOCUMENT SYSTEM "ch03_10.dtd" [   <!ELEMENT PRODUCT (PRODUCT_ID)>   <!ELEMENT PRODUCT_ID (#PCDATA)>   ]>  <DOCUMENT>     <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>                     <PRODUCT_ID>                         198348209                     </PRODUCT_ID>                 </PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             .             .             .             <ITEM>                 <PRODUCT>                     <PRODUCT_ID>                         198348206                     </PRODUCT_ID>                 </PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER> </DOCUMENT> 

And that's all it takes; now this DTD uses both internal and external parts .

If It's Both Internal and External, Which Takes Precedence?

Theoretically, if an element or attribute is defined in both an internal and external DTD, the definition in the internal DTD is supposed to take precedence, overwriting the external definition. Things were arranged that way to let you customize external DTDs as you like. However, my experience is that most XML processors simply consider it an error if there is an element or attribute conflict between internal and external DTDs, and they usually just halt.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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