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

homelectshws
D2Lbreeze (snow day)

lect38-dtd-entities
DTD - Entities
ch07

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. (We won't discuss “parameter entities”, which are entitities that don't render to part of a document; they render to part of an (enclosing) DTD.)


(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.

homelectshws
D2Lbreeze (snow day)


©2014, Ian Barland, Radford University
Last modified 2014.Nov.21 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.