XML technologies / XML / XML in a Nutshell / Document type definitions (DTDs) / External unparsed entities and notations

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

Not all data is XML. Many ASCII text files make no attempt to escape < as &lt; or obey any of XML’s other constraints. JPEG photographs, GIF graphics, QuickTime movies, MIDI sound files and so on are probably even more common. None are well-formed XML, yet they are important parts of many documents.

XML’s proposed mechanism for embedding such material is the external unparsed entity. The DTD specifies a name and URL for the entity containing non-XML data. For example, this ENTITY declaration associates the name turing_der_aus_dem_bus_steigt with the JPEG image at "http://www.turing.org.uk/turing/pi1/busgroup.jpg":

<!ENTITY turing_der_aus_dem_bus_steigt SYSTEM "http://www.turing.org.uk/turing/pi1/busgroup.jpg" NDATA jpeg>

Notations

Since the preceding data is not XML, the declaration NDATA specifies its type. Here, for example, we use jpeg. XML does not recognize this as an image format defined by the Joint Photographs Experts Group. It is merely a notation name declared somewhere in the DTD with a declaration NOTATION, as shown here:

 <!NOTATION jpeg SYSTEM "bild/jpeg"> 

Here we used the MIME type bild/jpeg as the notation’s external identifier. However, there is absolutely no standard, or even proposal, for its form. Each application must define its own requirements for notation contents and meaning.

Embedding unparsed entities in documents

The DTD only declares an unparsed entity’s existence, location and type. To use it at one or more places in a document, add an element with an attribute of type ENTITY whose value names the unparsed entity declared in the DTD. Do not use an entity reference such as &turing_der_aus_dem_bus_steigt;. References can point only to parsed entities.

Suppose the element bild and its attribute quelle are declared like this:

<!ELEMENT bild EMPTY>
<!ATTLIST bild quelle ENTITY #REQUIRED>

Then this element bild refers to the photograph at "http://www.turing.org.uk/turing/pi1/busgroup.jpg":

 <bild quelle="turing_der_aus_dem_bus_steigt"/> 

XML guarantees no particular behavior when an application encounters such an entity. It may not display the image. The parser may run without any user, may not know it is an image, and may never download it or contact its server. At best, it tells the application that an unparsed entity exists at a particular URL with a particular notation, leaving the application to decide what, if anything, to do.

Note: Unparsed general entities are not the only practical way to embed non-XML content. A plain URL, perhaps combined with an XLink, works well for many purposes, as HTML shows without needing unparsed entities. Keeping all information in one empty element such as <bild quelle="http://www.turing.org.uk/turing/pi1/busgroup.jpg"/> is clearly preferable to splitting it between an element and the document’s DTD. Only the notation is really added by an unparsed entity, and it is too poorly standardized to be very useful.
Many experienced XML developers consider unparsed entities a complicated, confusing mistake that should never have entered XML. Nevertheless, they are part of the specification and are therefore described here.

  

 

 

 

back to top