Making the Content for Delivery


So now that you have AxKit and all the associated components properly configured, you are ready to create some content for delivery. For this example, let's say you work for an online company that hosts resum s for job seekers.Your company is very forward-thinking, and all the resum s are stored in XML.Your task is to use Axkit, so that the original resum s in XML can be displayed in a web browser (that is, in HTML) upon request. To do this, you must transform the original XML data to HTML using XSLT. First, let's take a look at the format of the XML document that your application will be serving. A DTD for the resum is shown in Listing 9.1.

Listing 9.1 DTD that describes a resum . (Filename: ch9_resume.dtd)
 <?xml version="1.0" encoding="UTF-8"?>  <!ELEMENT resume (name, contact, education, experience)>  <!ELEMENT name EMPTY>  <!ATTLIST name     first CDATA #REQUIRED     middle CDATA #REQUIRED     last CDATA #REQUIRED>  <!ELEMENT contact (address, phone_number)>  <!ELEMENT address (street,city,state,zip)>  <!ELEMENT street (#PCDATA)>  <!ELEMENT city (#PCDATA)>  <!ELEMENT state (#PCDATA)>  <!ELEMENT phone_number (#PCDATA)>  <!ELEMENT education (school, degree)>  <!ELEMENT school (#PCDATA)>  <!ELEMENT degree (#PCDATA)>  <!ELEMENT experience (work+)>  <!ELEMENT work (company, job)>  <!ELEMENT company (#PCDATA)>  <!ELEMENT job (#PCDATA)> 

As you can see from the DTD, it describes the contents of a basic resum . The root element is the <resume> element. Each <resume> element contains a <name> , <contact> , <education> , and <experience> element. The corresponding XML schema for an XML resum document is shown in Listing 9.2.

Listing 9.2 XML schema that defines the XML resum . (Filename: ch9_resume.xsd)
 <?xml version="1.0" encoding="UTF-8"?>  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="qualified">     <xs:element name="address">        <xs:complexType>           <xs:sequence>              <xs:element ref="street"/>              <xs:element ref="city"/>              <xs:element ref="state"/>              <xs:element ref="zip"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="city" type="xs:string"/>     <xs:element name="company" type="xs:string"/>     <xs:element name="contact">        <xs:complexType>           <xs:sequence>              <xs:element ref="address"/>              <xs:element ref="phone_number"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="degree" type="xs:string"/>     <xs:element name="education">        <xs:complexType>           <xs:sequence>              <xs:element ref="school"/>              <xs:element ref="degree"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="experience">        <xs:complexType>           <xs:sequence>              <xs:element ref="work" maxOccurs="unbounded"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="job" type="xs:string"/>     <xs:element name="name">        <xs:complexType>           <xs:attribute name="first" type="xs:string" use="required"/>           <xs:attribute name="middle" type="xs:string" use="required"/>           <xs:attribute name="last" type="xs:string" use="required"/>        </xs:complexType>     </xs:element>    <xs:element name="phone_number" type="xs:string"/>     <xs:element name="resume">        <xs:complexType>           <xs:sequence>              <xs:element ref="name"/>              <xs:element ref="contact"/>              <xs:element ref="education"/>              <xs:element ref="experience"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="school" type="xs:string"/>     <xs:element name="state" type="xs:string"/>     <xs:element name="street" type="xs:string"/>     <xs:element name="work">        <xs:complexType>           <xs:sequence>              <xs:element ref="company"/>              <xs:element ref="job"/>           </xs:sequence>        </xs:complexType>     </xs:element>     <xs:element name="zip" type="xs:string"/>  </xs:schema> 

The XML resum document that we want to serve is shown in Listing 9.3 (ch9_resume.xml). Note that this is a simple example for demonstrating web delivery using AxKit.

Listing 9.3 XML resum that contains source data for web delivery. (Filename:ch9_resume.xml)
 <?xml version="1.0"?>  <?xml-stylesheet href="ch9_resume.xslt" type="text/xsl"?>  <resume>     <name first="John" middle="R" last="Smith"/>     <contact>        <address>           <street>2524 Samson Street</street>           <city>Southfield</city>           <state>MI</state>           <zip>48076</zip>        </address>       <phone_number>248-555-8587</phone_number>     </contact>     <education>        <school>Wayne State University (1992-1996)</school>        <degree>Bachelors of Science in Computer Science</degree>     </education>     <experience>     <work>        <company>Unravelnet Software (1996-1998)</company>         <job>Designed and developed networking applications using Perl, Java, and XML.         </job>      </work>      <work>         <company>Ford Motor Company (1998-Present)</company>         <job>Designed and developed web based applications using Perl, DBI, XML, Oracle, graphics/ccc.gif and Apache         </job>      </work>     </experience>  </resume> 

The first step is to create an XSLT stylesheet to transform this content to HTML when the client browser requests it. The XSLT stylesheet used to perform the transformation is shown in Listing 9.4.

Listing 9.4 XSLT stylesheet used to transform the XML resum to HTML. (ch9_resume.xslt)
 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="resume">        <html>           <body>              <center>                 <h2>                    <xsl:value-of select="name/@first"/>&#xA0;                    <xsl:value-of select="name/@middle"/>&#xA0;                    <xsl:value-of select="name/@last"/></h2>                    <xsl:value-of select="contact/address/street"/>                    <br/>                   <xsl:value-of select="contact/address/city"/>&#xA0;                    <xsl:value-of select="contact/address/state"/>&#xA0;                    <xsl:value-of select="contact/address/zip"/>                    <br/>                    <xsl:value-of select="contact/phone_number"/>              </center>              <br/>              <b>                 <u>Education</u>              </b>              <br/>              <xsl:value-of select="education/school"/>              <br/>              <xsl:value-of select="education/degree"/>              <br/>              <br/>              <b>                 <u>Experience</u>              </b>              <br/>              <xsl:apply-templates select="experience"/>           </body>        </html>     </xsl:template>     <xsl:template match="experience">        <xsl:for-each select="work">           <b>              <xsl:value-of select="company"/></b>           <br/>           <xsl:value-of select="job"/>           <br/>           <br/>        </xsl:for-each>     </xsl:template>  </xsl:stylesheet> 

You will now place both of the ch9_resume.xml and ch9_resume.xsl files into the axkit-stuff directory. At this point, everything has been configured and all the required files should be in place. How do we get AxKit to perform the XML transformation for us? Just request the ch9_resume.xml file through the browser with the URL http://localhost/axkit-stuff/ch9_resume.xml . The server identifies this file as needing to be processed with AxKit and proceeds with the transformation. It uses ch9_resume.xslt, specified in ch9_resume.xml, as the stylesheet. After the XML file is processed (transformed), the resulting HTML is sent to the browser. The generated HTML document is shown in Listing 9.5.

Listing 9.5 HTML resum document generated by AxKit. (Filename: ch9_resume.html)
 <html>     <body>        <center>           <h2>John R Smith</h2>              2524 Samson Street             <br>             Southfield MI 48076             <br>             248-555-8587        </center>        <br>        <b><u>Education</u></b>        <br>        Wayne State University (1992-1996)        <br>        Bachelors of Science in Computer Science        <br><br>        <b><u>Experience</u></b>        <br>        <b>Unravelnet Software (1996-1998)</b>        <br>        Designed and developed networking applications using Perl, Java, and XML.        <br><br>        <b>Ford Motor Company (1998-Present)</b>        <br>        Designed and developed web based applications using Perl, DBI, XML, Oracle, and graphics/ccc.gif Apache     <br>     <br>     </body>  </html> 

The HTML document generated by AxKit (and shown in Listing 9.5) is displayed in a browser as shown in Figure 9.2.

Figure 9.2. The HTML formatted resum generated by AxKit.

graphics/09fig02.gif

As you can see, the XML resum was transformed to HTML and sent to the browser, nicely formatted for display. AxKit also enables you to specify multiple stylesheets, which process the document in a SAX pipeline. This enables you to apply a transformation on the result of a previous transformation.

As I have just demonstrated, AxKit can easily automate the delivery of XML content by performing on-the-fly transformations. In addition, AxKit can also support caching and compression for more efficient delivery of the generated documents.



XML and Perl
XML and Perl
ISBN: 0735712891
EAN: 2147483647
Year: 2002
Pages: 145

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