While
you enter data directly into an XML document, you may find it more efficient
to store data in multiple files. One of the strengths of XML is the
ability to use entities to access external data. An XML document is
the most basic entity called a document entity, however, entities can
also refer to a text string, a DTD, an element or attribute declaration
or an external file containing character or binary data. You declare
entities in a DTD. The syntax depends on how the entity is classified.
The factors involved in classifying an enitity included 1) the content
of the entity; 2) how the entity is construction; and 3) where the definition
of the entity is located.
An entity that is part of the XML document's content is called a general
entity and are often used as placeholders for text strings that repeat
throughout the document or in other documents. An entity that is not
part of the document's content is called a parameter entity and are
used to store the various declarations found in a DTD.
If an entity is constructed using well-formed XML text, it is a parsed
entity. Non-XML entities are referred to as unparsed. Graphic files
are common examples of unparsed entities. If the entity can be defined
with a text string within the document's DTD, it's an internal entity.
If the definition relies on an external file, it's an external entity.
General Parsed Entities
General entities are declared in the DTD as follows:
<!ENTITY entity "value">
Examples without and with XML tags:
<!ENTITY Acme "Acme Products">
<!ENTITY Acme "<Company>Acme Products</Company>">
Once you've declared an entity, you can reference it anywhere in your
document as follows:
&entity;
Example
<Title>This is the home page of &Acme;.</Title>
For obvious reasons, you cannot use the & symbol as part of your
entity's value.
To declare a general external entity use the following syntax:
<!ENTITY entity SYSTEM "URL">
Example
<!ENTITY headlines SYSTEM "http://domain.com/filename.xml">
USED:
<News>&headlines;</News>
Work
with General Parsed Entities
Download lab files.
Open Items.dtd in Notepad and examine the contents.
Edit Orders.xml and add the following code:
<!DOCTYPE Customers SYSTEM "Items.dtd"
[
<!ELEMENT Customers (Customer+)>
<!ELEMENT Customer (Name, Address, Phone, E-mail?, Orders)>
<!ATTLIST Customer CustID ID #REQUIRED>
<!ATTLIST Customer CustType (home | business) #IMPLIED>
<!ELEMENT Name (#PCDATA)>
<!ATTLIST Name Title (Mr. | Mrs. | Ms.) #IMPLIED>
<!ELEMENT Address (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT E-mail (#PCDATA)>
<!ELEMENT Orders (Order+)>
<!ELEMENT Order (Order_date, Items)>
<!ATTLIST Order OrderID ID #REQUIRED>
<!ATTLIST Order OrderBy IDREF #REQUIRED>
<!ELEMENT Items (Item+)>
<!ELEMENT Order_date (#PCDATA)>
<!ELEMENT Item (#PCDATA)>
<!ATTLIST Item ItemPrice CDATA #REQUIRED>
<!ATTLIST Item ItemQty CDATA "1">
]>
...
<Items>
<Item ItemPrice="599.95">&DCT3Z;</Item>
<Item ItemPrice="199.95">&SM128;</Item>
<Item ItemPrice="29.95" ItemQty="2">&RCL;</Item>
</Items>
...
<Items>
<Item ItemPrice="59.95">&BCE4L;</Item>
</Items>
...
<Items>
<Item ItemPrice="59.99">&WBC500;</Item>
<Item ItemPrice="5.95" ItemQty="2">&RCA;</Item>
</Items>
...
<Items>
<Item ItemPrice="179.99" ItemQty="3">&SCL4C;</Item>
</Items>
Save and view the file in the browser.