Chapter 8 Working with XML Documents


Working with entities

In an XML document, an entity is a holder for some information. To use entities, you must first declare them, and then reference them. You declare entities in the DTD. The entities section of the XML document may be held separately or placed at the beginning of the document. Here is a simple DTD that specifies the two entities PQ and Logo.

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE Customers [
<!ENTITY PQ "Province of Quebec">
<!ENTITY Logo SYSTEM "nautilus.gif" NDATA GIF>
]>
<Customers><Customer><FName>Jess...

There are two kinds of entity declared here: PQ is a simple text replacement, and Logo is a reference to a graphic, held in a GIF file.

Entity types

The following kinds of entity can occur in XML documents:

Working with internal entities

You can include references to internal entities in the text. For example, the following element includes a predefined internal entity:

<Names>Sammy &amp; Rosie</Names>

The Names element has three children, the first being a text node, the second being an entity reference node, and the third being another text node. The firstChild of the entity reference node is a text node, and the nodeValue of this text object is the replacement text.

The entity definitions of predefined entities are also available from the entities property of the DOMDocument object's doctype property.

Working with external entities

The following is a complete XML document that defines and references an external entity:

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE DOC [
<!NOTATION gif SYSTEM "GIF">
<!ENTITY Logo SYSTEM "nautilus.gif" NDATA gif>
]><DOC><GRAPHIC ENTITY="Logo"/></DOC>

The following DynaScript function takes the DOMDocument object as its argument, and displays the nautilus.gif file that holds the Logo image:

function displayGraphic( domDoc){
dtd = domDoc.doctype;
entlist = dtd.entities;

elemlist = domDoc.getElementsByTagName( "GRAPHIC" );
elem = elemlist.item(0);
gif = elem.getAttribute( "ENTITY" );
ent = entlist.item( gif );
sysid = ent.systemId;
document.writeln( '<img src="' + sysid + '>');
}

Notes

 


Copyright © 1999 Sybase, Inc. All rights reserved.