10.8 Accessing External Entities

   

XML documents often reference entities, which are typically text that is reused among one or more XML documents. Entities can be internal, meaning they are defined within the XML document that uses them, or external, meaning they are defined elsewhere. The JSTL <x:parse> and <x:transform> actions support external entities with attributes that specify URIs that point to an entity's file.

The Web application shown in Figure 10-7 references an external entity that specifies the owner of the Rolodex , which is defined in rolodex.xml .

Figure 10-7. Accessing External Entities

graphics/10fig07.jpg

External entities are specified with a Document Type Definition (DTD). Listing 10.16 lists a modification of the Rolodex XML file listed in Listing 10.1 on page 424 that specifies a DTD that references an external entity.

The external entity specified in the preceding DTD is named owner and is defined in the file owner.xml . That file is listed in Listing 10.17.

If you use the <x:parse> action to parse an XML document that contains external entities, you can specify the URI for those entities with the <x:parse> systemId attribute, as illustrated by the JSP page listed in Listing 10.18.

The systemId attribute specified in the preceding JSP page points to the URI of the Web application because the external entity's file ” owner.xml ”resides in the top-level directory of the Web application.

The XSLT stylesheet used by the preceding JSP page to transform the Rolodex XML document into an HTML table is listed in Listing 10.19.

The preceding stylesheet is nearly identical to the stylesheet listed in Listing 10.6 on page 446, except that the preceding stylesheet displays the owner of the Rolodex, which is specified with the owner external entity in the Rolodex XML document.

Listing 10.16 rolodex.xml (Specifying a DTD)
 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE rolodex [    <!ELEMENT rolodex              (owner,contact,firstName,lastName,email,phone)>    <!ELEMENT owner     (#PCDATA)>    <!ELEMENT contact   (#PCDATA)>    <!ELEMENT firstName (#PCDATA)>    <!ELEMENT lastName  (#PCDATA)>    <!ELEMENT company   (#PCDATA)>    <!ELEMENT email     (#PCDATA)>    <!ELEMENT phone     (#PCDATA)>    <!ATTLIST phone home CDATA #REQUIRED>    <!ATTLIST phone work CDATA #IMPLIED>  <!ENTITY owner SYSTEM "owner.xml">  ]> <rolodex>  <owner>&owner;</owner>  <contact>       <firstName>Anna</firstName>       <lastName>Keeney</lastName>       <email>anna.keeney@worldlink.net</email>       <company>BSC, Inc.</company>       <phone type="work">716-873-9644</phone>       <phone type="home">716-834-8772</phone>    </contact>    <contact>       <firstName>Lynn</firstName>       <lastName>Seckinger</lastName>       <company>Sabreware, Inc.</company>       <email>lynn.seckinger@telecom.net</email>       <phone type="work">716-219-2012</phone>    </contact>    <contact>       <firstName>Ronald</firstName>       <lastName>Dunlap</lastName>       <company>World Traders, Inc.</company>       <email>ron.dunlap@worldlink.net</email>       <phone type="work">915-783-6494</phone>       <phone type="home">915-843-8727</phone>    </contact> </rolodex> 
Listing 10.17 owner.xml
 <!-- The name of the Rolodex owner --> Sabreware, Inc. 
Listing 10.18 index.jsp (Accessing External Entities)
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>    <head>       <title>Accessing External Entities</title>    </head>    <body>       <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>       <%@ taglib uri='http://java.sun.com/jstl/xml'  prefix='x' %>       <%-- Import the XML file and XSLT stylesheet --%>       <c:import var='rolodex_xml' url='rolodex.xml'/>       <c:import var='rolodex_xsl' url='rolodex.xsl'/>       <%-- Parse the XML File --%>       <x:parse var='document' xml='${rolodex_xml}'  systemId='http://localhost/core-jstl/xml/external-entities/  '       />       <%-- Perform the transformation --%>       <x:transform xml='${document}' xslt='${rolodex_xsl}'/>    </body> </html> 
Listing 10.19 rolodex.xsl (Generating HTML for Root Document)
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                   version="1.0">    <!-- Generate HTML for the document root -->    <xsl:template match="/">       <table border='1'>          <tr>             <th>First Name</th>             <th>Last Name</th>             <th>Company</th>             <th>Email</th>             <th>Work Phone</th>             <th>Home Phone</th>             <xsl:apply-templates/>          </tr>       </table>    </xsl:template>  <xsl:template match="owner">  <p>          <font size='5'>             Rolodex for  <xsl:value-of select='.'/>  </font>       </p>    </xsl:template>    <!-- Create a table row for each item and apply templates -->    <xsl:template match="contact">       <tr><xsl:apply-templates/></tr>    </xsl:template>    <!-- Create table data for each item and apply templates -->    <xsl:template match="contact/*">       <td><xsl:apply-templates/></td>    </xsl:template> </xsl:stylesheet> 
   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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