Creating External General Entities

Entities can also be external, which means you should provide a URI directing the XML processor to the entity. You can use references to external entities to embed those entities in your document. As we'll see near the end of this chapter, you can also indicate that an external entity should not be parsed, which means that you can associate binary data with a document (much like associating images with an HTML document).

External entities can be simple strings of text, entire documents, or sections of documents. All that matters is that when they are inserted into the document's content, the XML processor is satisfied that the document is well formed and valid.

As with DTDs, you can declare external entities using the SYSTEM or PUBLIC keywords. Entities declared with the SYSTEM keyword are for private use by an organization of individuals. Entities declared with PUBLIC are public, so they need a formal public identifier (FPIsee Chapter 3, "Valid Documents: Creating Document Type Definitions," for the rules on creating FPIs). Here's how you use the SYSTEM and PUBLIC keywords to declare an external entity:

 <!ENTITY  NAME  SYSTEM  URI  >  <!ENTITY  NAME  PUBLIC  FPI   URI  > 

For example, say that you've stored a date as the text October 15, 2003 in a file named ch04_03.xml. Here's how you could set up an entity named TODAY connected to that file:

Listing ch04_03.xml
 <!ENTITY TODAY SYSTEM "ch04_03.xml"> 

And here's how you could use a reference to that entity to insert the data into a document's content (note that I changed the value of the standalone attribute from "yes" to "no" here because we're working with an external entity):

Listing ch04_04.xml
 <?xml version = "1.0" standalone="no"?> <!DOCTYPE DOCUMENT [ <!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 PRODUCT (#PCDATA)> <!ELEMENT NUMBER (#PCDATA)> <!ELEMENT PRICE (#PCDATA)>  <!ENTITY TODAY SYSTEM "ch04_03.xml">  ]> <DOCUMENT> <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>  <DATE>&TODAY;</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>     <CUSTOMER>         <NAME>             <LAST_NAME>Jones</LAST_NAME>             <FIRST_NAME>Polly</FIRST_NAME>         </NAME>  <DATE>&TODAY;</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>     </CUSTOMER>     <CUSTOMER>         <NAME>             <LAST_NAME>Weber</LAST_NAME>             <FIRST_NAME>Bill</FIRST_NAME>         </NAME>  <DATE>&TODAY;</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>     </CUSTOMER> </DOCUMENT> 

Note how powerful this technique is: Now you can create documents that are themselves pieced together from other documents. If you wanted to use a public entity instead of a private one, you could use the SYSTEM keyword with a formal public identifier (FPI), like this:

 <!ENTITY TODAY SYSTEM "-//starpowder//Custom Entity Version 1.0//EN" "ch04_03.xml"> 

Defining external entities makes them available for multiple documents, which is useful in case you want to be able to have, say, the same text appear as a signature in all your documents, or work with text that will change frequently (such as a greeting for the day) that you want to edit in only one place.

Here's another note: Often nonvalidating XML processors will read a DTD to pick up any entity declarations you might have put there, even though they don't use the DTD to validate the document. That means that XML authors sometimes even add partial DTDs to documents that would not be considered valid so that they can use entity references. Here's an example (note that this DTD is not complete by any means, and that it carries only the declaration for the entity TODAY ):

 <?xml version = "1.0" standalone="no"?>  <!DOCTYPE DOCUMENT [  <!ENTITY TODAY SYSTEM "ch04_03.xml">  ]> <DOCUMENT> <CUSTOMER>         <NAME>             <LAST_NAME>Smith</LAST_NAME>             <FIRST_NAME>Sam</FIRST_NAME>         </NAME>  <DATE>&TODAY;</DATE>  <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>             .             .             . </DOCUMENT> 


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