Additional AxKit Tools


AxKit includes a large number of tools. Unfortunately, I can't discuss all the features in detail (that would require another book); however, in the next section, I discuss the most widely used options.

XPathScript

XPathScript is a stylesheet language for transformations that can act as a replacement for XSLT that was written by Matt Sergeant. The advantage of XPathScript over XSLT is that it enables you to embed Perl and XPath expressions within XML documents. This combines the power of XSLT with a programming language for dynamic content document creation comparable to Active Server Pages (ASP), JavaServer Pages (JSP), and PHP.

XPathScript is used the same way you use an XSLT stylesheet. The XPathScript stylesheet is specified using the <?xml-stylesheet...?> construct in the XML document. Let's modify the XML document used in the previous example to use XPathScript and demonstrate how it works. To demonstrate the dynamic capability of XPathScript, we'll add today's local time to the bottom of the output.

We'll first modify the ch9_resume.xml file to include the new stylesheet path . Let's replace this line in the XML document containing the resume:

 <?xml-stylesheet href="ch9_resume.xslt" type="text/xsl"?> 

with the following:

 <?xml-stylesheet href="ch9_resume.xps" type="application/x-xpathscript"  title="default" alternate="no"?> 

We now need to write XPathScript to transform the resume.xml (shown in Listing 9.6) and render proper output similar to our previous one (shown in Listing 9.7).

Listing 9.6 XPathScript used to transform the XML document containing a resum . (Filename: ch9_resume.xps)
 <html>     <body>        <center>           <h2>  <%= join(' ', findvalue('/resume/name/@first'),   findvalue('/resume/name/@middle'),   findvalue('/resume/name/@last'));   %>  </h2>  <%= findvalue('resume/contact/address/street/text()') %>  <br/>  <%= join(' ', findvalue('/resume/contact/address/city/text()'),   findvalue('/resume/contact/address/state/text()'),   findvalue('/resume/contact/address/zip/text()'));   %>  <br/>  <%= findvalue('/resume/contact/phone_number/text()') %></center>  <br/>           <b><u>Education</u></b>           <br/>  <%= findvalue('/resume/education/school/text()') %>  <br/>  <%= findvalue('/resume/education/degree/text()') %>  <br/><br/>           <b><u>Experience</u></b>           <br/>  <% my @nodes = findnodes('/resume/experience/work');   foreach my $node (@nodes) {   print "<b>".findvalue('./company/text()', $node)."</b>";  print "<br/>";                print findvalue('./job/text()', $node);                print "<br/><br/>";              }           %>           <br/>           <br/>           <% print "<b>Generated on: </b>";              my $date = localtime();              print $date;           %>     </body>  </html> 

As you can see, this is very similar to the XSLT stylesheet that we used earlier; however, there are a few important differences. For example, you can see following delimiters <% and %> . XPathScript follows the basic ASP approach of inserting code into a script. Note that we have inserted standard Perl code in between the <% and >% delimiters, and all the code is in bold typeface.

The XPathScript needs to be stored in the same directory as the ch9_resume.xml document. Because we modified the <?xml-stylesheet...?> directive in the XML document, we get the HTML output shown in Listing 9.7 by calling http://localhost/axkit-stuff/ch9_resume_xps.xml from the browser.

Listing 9.7 Generated HTML document that includes the local time. (Filename: ch9_resume_xps.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 Apache<br/><br/>        <br/>        <br/>        <b>Generated on: </b>Mon Jun  3 19:10:51 2002     </body>  </html> 

The output is similar to the output generated with the ch9_resume.xslt file using XSLT. The main difference is that we utilized the power of Perl and added the current localtime() to the output. As you can see in Figure 9.3, I added a footer, Generated on: , that displays the current date and time and is dynamically generated every time you reload the page.

Figure 9.3. Output resum viewed in a browser XPathScript generated local time.

graphics/09fig03.gif

eXtensible Server Pages

Another related technology supported by AxKit is eXtensible Server Pages (XSP). XSP is an XML-based technology that supports embedded code on a web server. So, XSP enables you to define and embed custom tags within the XML documents. These custom tags are executed each time the page is requested . XSP works similarly to Macromedia ColdFusion, for those who are familiar with that technology.

A small example of an XSP page, shown in Listing 9.8, demonstrates just how useful this utility can be. We'll use a small comma-delimited file of names , parse them, and output them from within our XSP-based application.

Listing 9.8 Input source data for an XSP-based application. (Filename: ch9_names.csv)
 Ilya,Sterin  Mark,Riehl  Jennifer,Eberhardt  Elise,Walter 

In this case, our input file is a simple CSV file that we can easily parse using Perl's split() function. Our task here is to use a simple application to parse the contents of a CSV file and display the contents in an HTML file. Listing 9.9 shows an XSP document that contains embedded Perl code that is executed whenever the document is accessed.

Listing 9.9 XSP file used to parse and print the contents of a CSV input file. (Filename: ch9_names.xsp)
 <?xml version="1.0"?>  <xsp:page xmlns:xsp="http://www.apache.org/1999/XSP/Core"                xmlns="uri://axkit.org/NS/MyHomePage"                language="Perl">  <html>    <body>      <h1>XML and Perl Book</h1>      <b><u>Authors and Staff</u></b>      <br/>       <xsp:logic><![CDATA[          open FILE, "names.csv";          my @lines = <FILE>;          foreach (@lines) {            print join(" ", split("," @lines))."\n<br/>";          }        ]]></xsp:logic>    </body>  </html>  </xsp:page> 

When called from the browser, as http://localhost/axkit-stuff/names.xsp , the HTML output appears as shown in Listing 9.10.

Listing 9.10 XSP-generated HTML document. (Filename: ch9_names_xsp.html)
 <?xml version="1.0" encoding="UTF-8"?>  <html>    <body>      <h1>XML and Perl Book</h1>      <b><u>Authors and Staff</u></b>      <br/>        Ilya Sterin        <br/>Mark Riehl        <br/>Jennifer Eberhardt        <br/>Elise Walter    </body>  </html> 

Viewed from within a browser, the output from Listing 9.10 appears as shown in Figure 9.4.

Figure 9.4. HTML document generated using XSP.

graphics/09fig04.gif



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