XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / External parsed general entities

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

The footer example is about the limit of what fits comfortably in a DTD. In practice, repeated website content is stored externally and inserted using PHP, server-side includes, or similar mechanisms. XML supports this through external general entity references, although the client rather than the server assembles the document parts.

An external parsed general entity reference is declared in the DTD with an ENTITY declaration, using the keyword SYSTEM and a URL for the replacement text instead of the text itself:

 <!ENTITY footer SYSTEM "http://www.oreilly.com/boilerplate/footer.xml"> 

Relative URLs are also common:

 <!ENTITY footer SYSTEM "/boilerplate/footer.xml"> 

When encountering the general entity reference &footer; in character data, the parser may replace it with the document at "http://www.oreilly.com/boilerplate/footer.xml" or in the file /boilerplate/footer.xml. External parsed entity references are forbidden in attribute values, which are usually small enough to use internal entities.

Note that we said the parser may replace the reference with the URL’s document, not that it must. Parsers have some implementation freedom here: validating parsers must expand such entities, while nonvalidating parsers may choose not to.

Not every text file can be an external entity. Its content must be capable of forming well-formed XML when inserted into a document. It need not itself have a root, but wrapping it in one must produce well-formed XML. Elements beginning in an entity must end in the same entity. An external entity also has no prolog, so cannot contain an XML declaration or document type declaration.

Text declarations

Instead of an XML declaration, an external entity may have a similar-looking text declaration. Its encoding declaration is required, while the attribute version is optional. There is also no standalone declaration. Its main purpose is telling the parser the entity’s character encoding. A typical text declaration is:

 <?xml version="1.0" encoding="MacRoman"?> 

A text declaration without a version attribute is also possible:

 <?xml encoding="MacRoman"?> 

The following example shows a well-formed external entity that another document can include using an external general entity reference.

<?xml encoding="ISO-8859-1"?>
<hr size="1" noshade="true"/>
<font CLASS="footer">
   <a href="index.html">O'Reilly Home</a> |
   <a href="sales/bookstores/">O'Reilly Bookstores</a> |
   <a href="order_new/">How to Order</a> |
   <a href="oreilly/contact.html">O'Reilly Contacts</a><br>
   <a href="http://international.oreilly.com/">International</a> |
   <a href="oreilly/about.html">About O'Reilly</a> |
   <a href="affiliates.html">Affiliated Companies</a>
</font>
<p><font CLASS="copy">Copyright 2004, O'Reilly Media, Inc.<br/><a href="mailto:webmaster@oreilly.com">webmaster@oreilly.com</a></font></p>

Code example: An external parsed entity

  

 

 

 

back to top