Perl
  Home arrow Perl arrow Page 5 - XSL Transformation with Perl
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 Transformation with Perl
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    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:
      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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    XSL Transformation with Perl - Error Management
    (Page 5 of 5 )

    Before I conclude this article, let me show you a final example. It incorporates some basic error handling that ensures that the end-user is not confronted with a screen of cryptic error messages. Incidently,  Perl is notoriously famous for generating such messages.

    # !/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 = eval { XML::XSLT->new ($xslfile, warnings => 1, debug => 0) };

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

    # transforms the XML file using the XSL style sheet
    eval { $xslt->transform ($xmlfile) };

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

    # send to output
    print $xslt->toString;w

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

    Let’s assume that the "portfolio.xsl" style sheet file has been deleted by an ignorant colleague. Without any error handling, the above Perl script should spit out the following error message:

    Error while parsing:
    syntax error at line 1, column 0, byte 0 at /usr/perl/site/lib/XML/Parser.pm line
    187

    portfolio.xsl at /usr/perl/site/lib/XML/XSLT.pm line 1507.

    Definitely, a sight for sore eyes -- not only is it cryptic and complicated, bu I’ll frankly admit that even I  was at a loss to understand the reasons behind the error. However, sanity prevailed after I added bits of the "error-handling", as seen in the listing above. Execute the Perl script in order to view the following output; I’ll continue to assume that the "portfolio.xsl" file has been deleted:

    Sorry, Could not create an instance of the XSL Processor using portfolio3.xsl.

    Alternatively, the script will spit out the following error message if the XML document was  not found at its specified location:

    Sorry, Could not transform XML file, portfolio3.xml.

    To be frank, there’s no rocket science behind this, just some deft manipulation using the "eval" function, as seen below.

    // snip

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

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

    // snip

     

    For the uninitiated, this "eval" function allows you to execute any Perl expression, and the results of the expression are stored in the  special Perl "$@"variable. A quick check on the state of this variable helps you to determine if an error has occurred; if it is null, then all is well. However, if something goes wrong, Perl will store the error message in this variable, thereby allowing you to dictate the next course of action.

    Conclusion

    This brings us to end of the first part of XSL Transformations with Perl. Today, I showed you how to get started with the "XML::XSLT" package that allows you to transform XML documents using XSLT style sheets using Perl. After a simple example demonstrating the serve() method of the XML::XSLT() object, I demonstrated the transform() method, which returns an XML::DOM() object. Finally, I showed you how to add some basic error handling to your scripts in order to ensure better error handling in your Perl scripts.

    In the next part, I shall show you how to play around with the XML::DOM() and XML::XSLT() objects in the same Perl script as well as demonstrate some fancy XSL Transformations that’ll keep you coming back for more. Till then, happy transform()ing!

    Note: All examples in this article have been tested on Linux/i586 with Perl 5.8.0. Examples are illustrative only, and are not meant for a production environment. YMMV!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    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 5 hosted by Hostway