Chapter 8 Working with XML Documents
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.
The following kinds of entity can occur in XML documents:
<!ENTITY PQ "Province of Quebec">
<Region type="Province">&PQ;</Region>
<!ENTITY chap1 SYSTEM "chapter1.xml">
<!ENTITY chap2 SYSTEM "chapter2.xml">
<BOOK>
&chap1;
&chap2;
</BOOK>
<!ENTITY Logo SYSTEM "emblem.gif" NDATA gif>
<GRAPHIC entity="Logo">
You can include references to internal entities in the text. For example, the following element includes a predefined internal entity:
<Names>Sammy & 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.
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 + '>');
}
DOMDocumentType
object, which
is accessible as the doctype
property
of the DOMDocument
object. The entities
property
of DOMDocumentType
returns
a list of entities.
systemID
property
of the DOMEntity
object
returns the path to the file.
img
tag
to a browser, as the browser does not recognize the home directory.
Copyright © 1999 Sybase, Inc. All rights reserved. |