XML

External Entities

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>

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.