Perl
  Home arrow Perl arrow XSL Transformations with Perl, Revisit...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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 Transformations with Perl, Revisited
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    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

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway