Perl
  Home arrow Perl arrow XSL Transformations with Perl, Revisited
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? 
Google.com  
PERL

XSL Transformations with Perl, Revisited
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-02-06


    Table of Contents:
  • XSL Transformations with Perl, Revisited
  • Objects in the mirror
  • Different strokes
  • Transforming the Transformed

  • 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 Transformations with Perl, Revisited
    ( Page 1 of 4 )

    This article covers some API methods of the XML::XSLT module and shows you how to create a new XML file without needing to do any Perl programming. You will also learn how to transform a dynamically generated XML data structure.

    Introduction

    Welcome to more XSL transformations with Perl!

    For the benefit of the latecomers: in the first article I wrote on this subject, I started with a comparison of the different XSLT processors available to Perl programmers on CPAN. Next, I showed you how to get started with the XML::XSLT module -- a XSLT processor developed in Perl. After a simple example in which I demonstrated the serve() method, I went on to explain how you can use the transform() method to achieve the same result. Finally, I concluded the article with a Perl script that implemented basic error handling to trap bugs that have a nasty habit of showing up when you least expect them to.

    Today, I will discuss some more API methods of the XML::XSLT module that help you get the job done. Then, I'll show you how to create a new XML file by applying some neat XSL transformations to an existing XML file without having to resort to any Perl programming. The final example will demonstrate how to transform a dynamically generated XML data structure -- the latter generated by another XSL transformation earlier in the same Perl script.

    Sounds interesting? You bet!

    Yet Another API Method

    There is more than one way to skin a cat; this age-old adage also holds good for the XML::XSLT module. Let me show you how!

    Earlier, I introduced the serve() and transform() methods, and just when you think that you've mastered them all, you'll be surprised to learn that the XML:XSLT module provides one more method to get the job done.

    Time to say hello to the process() method. Review the next code listing to learn how this new method works:

    # /usr/bin/perl

    # import required modules
    use XML::XSLT;
    use XML::DOM;

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

    # create an instance of XSL::XSLT processor
    my $xslt = eval { XML::XSLT->new($xslfile, warnings => 1, debug
    => 0) };

    # error handling here ...
    if ($@) {
           
    die("Sorry, Could not create an instance of the XSL
    Processor using $xslfile.\n");
    }

    # load XML
    eval { $xslt->open_xml($xmlfile) };

    # .. here ...
    if ($@) {
           
    die("Sorry, Could not load XML file, $xmlfile.\n");
    }

    # transform XML file
    eval { $xslt->process(debug => 0) };

    # .. and here.
    if ($@) {
            
    die("Sorry, Could not transform XML file, $xmlfile.\n");
    }

    # send to output
    print $xslt->toString;

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

    Note that I have reused the XML and XSLT documents from my previous article. However, as you can see above, the same cannot be said for the code that generates the output.

    You'll notice that the code listing starts along the same lines as my earlier examples. I've defined some local variables and created an instance of the XSLT processor. However, this is where the similarity ends. Take a peek at the next code snippet; it highlights the differences.

    // snip

    # load XML
    eval { $xslt->open_xml($xmlfile) };

    # .. here ...
    if ($@) {
            die("Sorry, Could not load XML file, $xmlfile.\n");
    }

    # transform XML file
    eval { $xslt->process(debug => 0) };

    # .. and here.
    if ($@) {
            die("Sorry, Could not transform XML file,
    $xmlfile.\n"); }

    // snip

    To refresh your memory, the earlier examples used the serve() or transform() methods of the XSLT Processor to load and transform the XML file in a single run. However, this example introduces the open_xml() method. This method allows me to load an XML file that I wish to transform.

    But, what about the transformation process? No sweat; the XSLT processor is equipped, as indicated above, with a process() method that helps me get the job done. Note that I can turn on debug and/or warning messages by setting appropriate flags, as I did with the other methods.



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek