External Entities

[Previous] [Next]

In this section, we'll look at the three categories of external entities: external parsed general entities, external unparsed general entities, and external parameter entities. External entities can be used when more than one DTD uses the same entities. You can reduce the amount of time it takes to produce new DTDs by creating a repository of documents containing entity declarations.

External Parsed General Entities

External parsed general entities enable you to store a piece of your XML document in a separate file. An external parsed general entity can be set equal to this external XML document. Using the external general entity, the external XML file can be referenced anywhere in your XML document.

Declaring an external parsed general entity

The syntax for declaring an external general entity is shown here:

<!ENTITY name SYSTEM URI>

Notice that the external general entity declaration uses a keyword following the entity name. This keyword can be SYSTEM or PUBLIC. The PUBLIC identifier is used when the document is officially registered. The SYSTEM identifier is used with unregistered documents that are located using a URI, which stands for Uniform Resource Identifier, to tell the parser where to find the object referenced in the declaration. Since we are now working with unregistered documents, we will use the SYSTEM identifier in the examples below.

Using external parsed general entities

External parsed general entities can be referenced in the document instance and in the content of another general entity. Unlike internal general entities, external parsed general entities cannot be referenced in an attribute value. To reference an external parsed general entity, you need to precede the entity with an ampersand and follow it with a semicolon, the same way you reference internal general entities. Let's look at how to use external parsed general entities in the XML document. Since our sample file HelpHTM.htm is a well-formed XML document, we can save it as Help.xml. To divide the Web page in this document into header, footer, left navigation bar, and body sections, add the following code to the Help.xml:

 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <!DOCTYPE html SYSTEM "StandXHTML.dtd" [ <!ENTITY topheader SYSTEM "Topheader.htm"> <!ENTITY leftnav SYSTEM "Leftnav.htm"> <!ENTITY footer SYSTEM "Footer.htm"> <!ENTITY body SYSTEM "Body.htm"> ]> <html> <head> <title>Northwind Traders Help Desk</title> </head> <body text="#000000" bgcolor="#FFFFFF" link="#003399" alink="#FF9933" vlink="#996633"> &topheader; &leftnav; &body; &footer; </body> </html> 

Using this new DTD, the Body.htm file referenced in our sample Web help page would look like this:

 <html> <p><a name="Top"><!--Top tag--></a></p> <table border="5" frame="" rules="" width="100%" cellspacing="0" cellpadding="0"> <tr> <td colspan="2" align="Center"> <cellcontent cellname="Help Topic List "> <h1 align="Center">Help Desk</h1> </cellcontent> </td> </tr> <tr valign="Top" > <td align="Left" > <cellcontent cellname="First-Time Visitor"> <ul > <font size="3"> <b>For First-Time Visitors</b> </font> <li> <p> <a href="FirstTimeVisitorInfo.html" target=""> First-Time Visitor Information</a> </p> </li> <li> <p> <a href="SecureShopping.html" target=""> Secure Shopping at Northwind Traders</a> </p> </li> <li> <p> <a href="FreqaskedQ.htm" target=""> Frequently Asked Questions</a> </p> </li> <li> <p> <a href="NavWeb.html" target=""> Navigating the Web</a> </p> </li> </ul> </cellcontent> </td> <td align="Left"> <cellcontent cellname="Shipping links"> <ul type=""> <font size="3"> <b>Shipping</b> </font> <li> <p> <a href="Rates.htm" target="">Rates</a> </p> </li> <li> <p> <a href="OrderCheck.htm" target=""> Checking on Your Order</a> </p> </li> <li> <p> <a href="Returns.htm" target=""> Returns</a> </p> </li> </ul> </cellcontent> </td> </tr> </table> </html> 

The Help.xml file and the Body.htm file are included on the companion CD. Similarly you can create three other external files: Topheader.htm, Leftnav.htm, and Footer.htm. All of the rules that apply to internal general entities also apply to the external parsed general entities. Only the declaration and the source of the replaced text are different.

External Unparsed General Entities

External unparsed general entities are similar to other entities, except that the XML parser will not try to parse the information within them. Essentially, the data within an external unparsed general entity is ignored by the XML parser and passed on to the application that is using the document in its original format. This is exactly what we want done for non-XML files such as images.

Notations

External unparsed general entities contain one additional component: notations. Notations are used by the application to identify the data in the external unparsed general entity or to identify what application needs to be used to interpret the data. For example, if the data contained in the entity is a GIF image file, the following notation would identify it:

 <!NOTATION GIF89a SYSTEM "-//Compuserve//NOTATION Graphic Interchange Format 89a//EN"> 

It would be up to the application to determine how to interpret this information and present the image properly.

Notations can be declared in two different ways. The first method is used when the notation is not public and is located at some URI. It uses the syntax shown here:

<!NOTATION notation_name SYSTEM resource_URI>

The second method is used for a notation that has been registered as public and given a unique ID. It uses the following syntax:

<!NOTATION notation_name PUBLIC public_ID resource_URI>

Examples of the two types of declarations are shown here:

 <!NOTATION GIF89a SYSTEM "-//Compuserver//NOTATION Graphic Interchange Format 89a//EN"> <!NOTATION GIF SYSTEM "GIF"> <!NOTATION BMP SYSTEM "MSPAINT.EXE"> <!NOTATION GIF89a PUBLIC "-//Compuserve//NOTATION Graphic Interchange Format 89a//EN" "ps4prp.exe"> 

Declaring an external unparsed general entity

Once you have created a notation, you can use the notation to declare external unparsed general entities. The format for these declarations is similar to the declarations for external parsed general entities, except that in this case a notation appears at the end of the declaration. The NDATA keyword is used to associate the external unparsed general entity with a particular notation. The syntax for the declaration is shown here:

<!ENTITY entity_name SYSTEM URI NDATA  notation_name>

Using our second notation definition, you could create the following declaration:

 <!ENTITY image.topnav SYSTEM "topnav.gif" NDATA GIF> 

Now that you have defined the notation and then defined an external unparsed general entity that uses this notation, you will want to use this external unparsed general entity in your XML document body. For example, you might want to insert this GIF image at the top of a Web page.

Using external unparsed general entities

When you are using an external unparsed general entity as a value for an attribute in your XML document, you will want the XML parser to ignore the data returned by the entity. To accomplish this, you must tell the XML parser that you are referencing an external unparsed general entity in the declaration of the attribute. The ENTITY or ENTITIES keyword will be used in the attribute declaration to mark an attribute as containing an external unparsed general entity reference, as shown here:

 <!--Part of the DTD--> <!NOTATION gif SYSTEM "gif"> <!NOTATION jpeg SYSTEM "jpg"> <!NOTATION bmp SYSTEM "bmp"> <!ENTITY image.topimage SYSTEM "topimage.gif" NDATA gif> <!ENTITY image.topnav1 SYSTEM "topnav1.gif" NDATA gif> <!ENTITY image.topnav2 SYSTEM "topnav2.gif" NDATA gif> <!ENTITY Welcome SYSTEM "Welcome.jpg" NDATA jpg> <!ELEMENT topimages EMPTY> <!ATTLIST topimages topimage ENTITY #FIXED "image.topimage" topnav ENTITIES "image.topnav1 image.topnav2"> <!ELEMENT img EMPTY> <!ATTLIST img %attrs; align CDATA #IMPLIED border CDATA #IMPLIED width CDATA #IMPLIED height CDATA #IMPLIED hspace CDATA #IMPLIED vspace CDATA #IMPLIED src ENTITY #REQUIRED type NOTATION (gif|jpg|bmp) "jpg"> <!--XML Body--> <topimages topimage="image.topimage" topnav="image.topnav1 image.topnav2"></topimages> <img src = "Welcome"></img> 

This code declares two elements: topimages and img. The topimages element has two attributes associated with it: topimage and topnav. The img element is the one used in the DTD example discussed in the "Rewriting the sample DTD using parameter entities" section, except that here it contains the type attribute. The type attribute is a notation attribute, as it contains the keyword NOTATION. The items listed in the enumerated type must be defined in the DTD as notations, as is done in the above declaration.

External Parameter Entities

External parameter entities are just like internal parameter entities except that they retrieve the replacement text from external files.

Declaring an external parameter entity

The syntax for declaring an external parameter entity is similar to the declarations for internal parameter entities, except that the SYSTEM keyword or the PUBLIC keyword is used. The syntax for the declaration is shown here:

<!ENTITY % name SYSTEM  "string_of_characters">

To use the external parameter entity, you could place all of the parameter entities that were defined in the example DTD in a file named Parameter.dtd. To do so, you would add the following code to the XML document:

 <!ENTITY % parameterentities SYSTEM "Parameter.dtd"> %parameterentities; <!--================ Document Structure=========================--> <!ELEMENT html (head , body)> <!ATTLIST html %i18n; xmlns CDATA #FIXED 'http://www.w3.org/1999/xhtml'> <!--Rest of DTD here--> 

First we declare the parameterentities entity, which links to the external Parameter.dtd, and then we use parameterentities to insert this document into the XML document. This external parameter entity could be used to create several DTDs. External parameter entities are useful when parts of your DTD will be used by several other DTDs.



Developing XML Solutions
Developing XML Solutions (DV-MPS General)
ISBN: 0735607966
EAN: 2147483647
Year: 2000
Pages: 115
Authors: Jake Sturm

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net