Perl
  Home arrow Perl arrow Page 3 - XSL Transformation with Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

XSL Transformation with Perl
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2005-07-25


    Table of Contents:
  • XSL Transformation with Perl
  • Getting started
  • My Investment Portfolio
  • Demystifying the XML::XSLT processor
  • Error Management

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    XSL Transformation with Perl - My Investment Portfolio
    ( Page 3 of 5 )

    Now that the installation process is out of the way, it's time to get our hands dirty with some code. Consider the following XML document (say "portfolio.xml") that lists the stocks in a sample investment portfolio:

    <?xml version="1.0"?>
    <portfolio>
        <name>The Bull Pit</name>
     <stock>
          <symbol>KO</symbol>
          <companyname>Coca-Cola</companyname>
          <quantity>500</quantity>
          <lasttradedprice>14.00</lasttradedprice>
     </stock>
    </portfolio>

    While XML-savvy programmers will appreciate the manner in which the data is hierarchically organized, most market analysts and advisers, who are more at home with the ubiquitous Microsoft Excel, will struggle to make any sense of this document. And unfortunately, it is your job to keep them happy so that you can pay your monthly bills. Hence, the need to find a solution that will help convert the above data to an "analyst-friendly" format.

    Consider the following output:

    Portfolio Name: The Bull Pit
    Stock(s):
    Company Name: Coca-Cola
    Symbol: KO
    Quantity: 500
    Last Traded Price: 14.00

    While this may not be fancy - or even useful - you’ll agree that any layman, let alone the stock analyst, will understand that the output is a listing of stocks in a portfolio. So, how would I bring about this transformation? As you might have guessed, the answer is XSL Transformations (or XSLT in short).

    Take a peek at the following XSLT style sheet (say "portfolio.xsl"):

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    Portfolio Name: <xsl:value-of select="portfolio/name" />
    Stock(s):
    Company Name: <xsl:value-of select="portfolio/stock/companyname" />
    Symbol: <xsl:value-of select="portfolio/stock/symbol" />
    Quantity: <xsl:value-of select="portfolio/stock/quantity" />
    Last Traded Price: <xsl:value-of select="portfolio/stock/lasttradedprice" />
    </xsl:template>
    </xsl:stylesheet>

    As mentioned earlier, I’ll assume that you are familiar with the nuances of the XSLT language. If not, it might be useful to refresh your memory by visiting some of the URLs listed below. But, before you do, don’t forget to bookmark this page and come back once you’re done!

    XSLT Tutorial at W3Schools: http://www.w3schools.com/xsl/default.asp

    XSLT Tutorial at Zvon.org: http://www.zvon.org/xxl/XSLTutorial/Books/Book1/

    XSLT & XPath Tutorial at TopXML.com: http://www.topxml.com/xsl/tutorials/intro/

    XSL Family at W3C:
    http://www.w3.org/Style/XSL/

    Hello, Perl!

    As the title suggests, this article is about XSL Transformations with Perl and so far, I have not yet written a single line of Perl code. It's time to rectify this little anomaly. Take a look at the next code listing: a simple Perl script that uses the aforementioned XML::XSLT package to transform the "portfolio.xml" document using the "portfolio.xsl" style sheet, both of which were listed in the previous section.

    # !/usr/bin/perl

    # import required modules
    use XML::XSLT;

    # define local variables
    my $xslfile = "portfolio.xsl";
    my $xmlfile = "portfolio.xml";

    # create an instance of XSL::XSLT processor
    my $xslt = XML::XSLT->new ($xslfile);

    # transform XML file and print output
    print $xslt->serve($xmlfile);

    # free up some memory
    $xslt->dispose();

    Execute the Perl script (say "portfolio.pl") listed above -- I’ll assume that the "portfolio.xml" and "portfolio.xsl" files are in the sample folder as the script -- and you should see the same "analyst-friendly" output listed in the previous section.

    It's time to turn our attention to the "portfolio.pl" script! For starters, I’ve imported the "XML::XSLT" module and defined a couple of variables to store the names of the XML and the XSL files. Now, things start to get interesting. First, I’ve created an instance of the XSLT processor by invoking the new() method of the XML::XSLT module. Observe that I have passed the name of the XSL style sheet file as an input parameter to the method -- a mandatory requirement.

    Next, I have invoked the serve() method  that does the dirty work of parsing the input XML document, applying the XSLT instructions from the style sheet and finally, print()ing the "transformed" output.

    Finally, like all good programmers, I invoke the dispose() method to clear up any memory occupied by the processor object.

    Simple and straightforward, wasn’t it?



     
     
    >>> More Perl Articles          >>> More By Harish Kamath
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT