RU beehive logo ITEC dept promo banner
ITEC 325
2019fall
ibarland

DTDs
entities

Originally based on XML Visual Quickstart Guide by Kevin Howard Goldberg, and notes therefrom by Jack Davis (jcdavis@radford.edu).

Aside: The meaning of quotes. (Entities are used in similar situations.)

Defining Entities

Entities are named-constants for your XML. With an entity, you define its name and the text it should expand into when referenced in an XML document. The entity reference is included in an XML document or DTD; it's replaced by defined-value upon rendering.

Define an entity whenever you want one “glyph” — something that always renders as the same text-characters. So <hellip; is one fine entity (which renders/is different from the three-character string “...”), but you might want to define an entity “<motto>” which is always the string “OnlineEd: Bringing the Web to <i>U</i>” (always capitalized that way, etc.).

We will discuss “general entities” here. There are other types of entities (“parameter entities”, and parsed/unparsed versions of general entities), but they all work in the same way, and they are all defined through a DTD. The differences lie in where the entity can be expanded, and what kind of data it contains. See the textbook for further info on these other types of entities.


(remaining topics are optional — not frequently used:)

Creating Entities for Unparsed Content

Unparsed entities are used to embed non-text or non-XML content into an XML document.

Note that current browsers (2012) don't [have XML parsers that] handle unparsed entities, even when specified correctly in the DTD.

The entities we've seen so far are called parsed entities because the XML parser looks at them and analyzes them in the course of parsing through the XML document (and recognizing them between “&” and “;”).

Unparsed entities, on the other hand, do not have entity references (that is, you won't actually use them between & and ;). Instead, you'll specify them as an attribute; that attribute-value needs to have been defined as ENTITY in the DTD.

For example, in the following we'll suppose we want to have an entity “lighthouse_pic” which resolves to a jpg.

  1. Create the data that you want to embed in the XML document. It may be, or contain, virtually anything — plain text, an image file, a video file, a PDF file, etc..
    In our case, we'll make a file lighthouse.jpg.
  2. In the DTD: create a “notation” (a category) for unparsed-entities of a certain type: This will be used to tell clients what format the data-referred-to will be in.
    <!NOTATION notationName SYSTEM "aMimeType" >
    Example: <!NOTATION jpg SYSTEM "image/jpeg">
  3. In the DTD: Now, make a specific entity using that notation: <!ENTITY lighthouse_pic SYSTEM "lighthouse.jpg" NDATA jpg>
    Note that the entity's type is a SYSTEM resource, and the NDATA specifies what “notation” to use.
  4. In the DTD: Now specify what tags are allowed to refer to this entity:
                        <!ELEMENT photo EMPTY>
    <!ATTLIST photo source ENTITY #REQUIRED>
  5. In the XML document, you can now refer to that resource (finally!): <photo source="lighthouse_pic" />
Putting this all together:
In the DTD:
                    <!ENTITY lighthouse_pic SYSTEM "lighthouse.jpg" NDATA jpg>
<!NOTATION jpg SYSTEM "image/jpeg">
<!ELEMENT photo EMPTY>
<!ATTLIST photo source ENTITY #REQUIRED>
In the XML document: <photo source="lighthouse_pic" />

Here is another example that uses parsed and unparsed general entitities:
Viewing the raw XML document (note that usually the raw xml tree should be shown, but most browsers will instead give an error on the entity they don't understand).
Here's the xml document after being transformed by using XSLT. Classic Cars 4 w/Photo Example

Parameter Entities

Parameter entities are used to reference external information in the DTD itself. However, they are not common in practice. You can see the text for details.


logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.