Creating Internal General Entities

As discussed in the beginning of the chapter, entities can be either internal or external. We already saw how to create an internal general reference in the beginning of the chapter, when we created an internal general entity named TODAY and referenced it as &TODAY; in the document:

 <?xml version = "1.0" standalone="yes"?>  <!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 "October 15, 2003">  ]> <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> 

You can see the results in Figure 4-2. There are a few things to note here; one is that you can nest general references like this:

 <!ENTITY NAME "Alfred Hitchcock">  <!ENTITY SIGNATURE "&NAME; 14 Mystery Drive"> 

Another point is that entity references can't be circular, or you'll drive the XML processor crazy. Here's an example:

 <!ENTITY NAME "Alfred Hitchcock &SIGNATURE;">  <!ENTITY SIGNATURE "&NAME; 14 Mystery Drive"> 

In this case, when the XML processor tries to resolve the &NAME; reference, it finds that it needs to substitute the text for the SIGNATURE entity in the text for the NAME entity. However, the NAME entity needs the SIGNATURE entity's text, and so on, around in a circle that never ends. The result is that circular entity references have been made illegal in valid documents.

Also, it's worth noting that you can't use general entity references to insert text that is supposed to be used only in the DTD and not in the document content itself. Here's an example of something that's considered illegal:

 <!ENTITY TAGS "(NAME,DATE,ORDERS)">  <!ELEMENT CUSTOMER &TAGS;> 

The correct way to do this is with parameter entities, not general entities, and I'll cover them in a few pages. You can use general entities in the DTD to insert text that will become part of the document body, however.



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