External Parameter Entities

When you use a parameter entity in the DTD's external subset, you can reference that entity anywhere in the DTD, including in element declarations. Here's an example. In this case, I'm using an external DTD named ch04_07.dtd for this document:

 <?xml version = "1.0" standalone="no"?>  <!DOCTYPE DOCUMENT SYSTEM "ch04_07.dtd">  <DOCUMENT>     <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Oranges</PRODUCT>                 <NUMBER>24</NUMBER>                 <PRICE>.98</PRICE>             </ITEM>             .             .             .             <ITEM>                 <PRODUCT>Asparagus</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Lettuce</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>     </CUSTOMER> </DOCUMENT> 

In the external DTD subset, ch04_07.dtd, I'm going to set things up so that the <DOCUMENT> element can contain not only <CUSTOMER> elements, but also <BUYER> and <DISCOUNTER> elements. Each of these two new elements, <BUYER> and <DISCOUNTER> , has the same content model as the <CUSTOMER> element (that is, these elements can contain <NAME> , <DATE> , and <ORDERS> elements); to save a little time, I'll assign that content model, (NAME,DATE,ORDERS) , to a parameter entity named record :

 <!ENTITY % record "(NAME,DATE,ORDERS)">  <!ELEMENT DOCUMENT (CUSTOMER  BUYER  DISCOUNTER)*>     .     .     . 

Now I'm free to refer to the record parameter entity where I like. In this case, that means using it to declare the <CUSTOMER> , <BUYER> , and <DISCOUNTER> elements:

Listing ch04_07.xml
 <!ENTITY % record "(NAME,DATE,ORDERS)"> <!ELEMENT DOCUMENT (CUSTOMER  BUYER  DISCOUNTER)*>  <!ELEMENT CUSTOMER %record;>   <!ELEMENT BUYER %record;>   <!ELEMENT DISCOUNTER %record;>  <!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 PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)> 

Now the document works and parses as expected: I can use <CUSTOMER> , <BUYER> , and <DISCOUNTER> elements inside the <DOCUMENT> element, and all three of those elements have the same content model:

Listing ch04_08.xml
 <?xml version = "1.0" standalone="no"?> <!DOCTYPE DOCUMENT SYSTEM "ch04_07.dtd"> <DOCUMENT>  <CUSTOMER>  <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>         <DATE>October 15, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Oranges</PRODUCT>                 <NUMBER>24</NUMBER>                 <PRICE>.98</PRICE>             </ITEM>         </ORDERS>  </CUSTOMER>   <BUYER>  <NAME>             <LAST_NAME>Jones</LAST_NAME>             <FIRST_NAME>Polly</FIRST_NAME>         </NAME>         <DATE>October 20, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Bread</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Apples</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>  </BUYER>   <DISCOUNTER>  <NAME>             <LAST_NAME>Weber</LAST_NAME>             <FIRST_NAME>Bill</FIRST_NAME>         </NAME>         <DATE>October 25, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Asparagus</PRODUCT>                 <NUMBER>12</NUMBER>                 <PRICE>.95</PRICE>             </ITEM>             <ITEM>                 <PRODUCT>Lettuce</PRODUCT>                 <NUMBER>6</NUMBER>                 <PRICE>.50</PRICE>             </ITEM>         </ORDERS>  </DISCOUNTER>  </DOCUMENT> 

This example points out probably the biggest reason people use parameter entities: to handle text that's repeated often in element declarations in a DTD. In this case, I specified the content model of three elements using the same parameter entity, but I could just have easily set up a parameter entity to let me specify an attribute list that was the same for as many elements as I like. In this way, you can control the declarations of many elements and attributes, even in a huge DTD. And if you need to modify a declaration, you need to modify only the parameter entity, not each declaration in detail.

For example, you might divide your attributes in a big DTD into various types. When you declare some new element, you might want to give it only the image-handling and URI-handling attributes, which you could do like this (in fact, this is the way the XHTML DTDs are built):

 <!ATTLIST NEW_ELEMENT %image_attributes; %URI_attributes;> 

Here's another example showing how to use parameter entities; in this case, I'm going to base my document on the XHTML 1.0 transitional DTD, adding a few elements of my own to XHTML. To do that, I declare the elements I want to use and then simply include the entire XHTML 1.0 transitional DTD using a parameter reference like this:

 <!ENTITY % record "(NAME,DATE,ORDERS)">  <!ELEMENT DOCUMENT (CUSTOMER  BUYER  DISCOUNTER)*> <!ELEMENT CUSTOMER %record;> <!ELEMENT BUYER %record;> <!ELEMENT DISCOUNTER %record;> <!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 PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)>  <!ENTITY % XHTML1-t.dtd PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. graphics/ccc.gif org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  %XHTML1-t.dtd; 


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