Perl
  Home arrow Perl arrow Page 2 - 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 - Objects in the mirror
    ( Page 2 of 4 )

    All examples, thus far, use a XML file as an input source to the XSLT processor. However, this may not always be true. Consider the following example that provides a XML DOM object as input:

    # !/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 the DOM object
    my $xml_parser = new XML::DOM::Parser;
    my $xml_dom = eval { $xml_parser->parsefile ($xmlfile) };

    # check for errors ...
    if ($@) {
            die("Sorry, Could not parse the XML file,
    $xmlfile.\n"); }

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

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

    # transform the XML DOM object using the XSL style sheet
    eval { $xslt->transform ($xml_dom) };

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

    # send to output
    print $xslt->toString;

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

    # throws error
    # $xslt->dispose();

    Once again, I've reused the "portfolio.xml" XML document as well as the "portfolio.xsl" XSLT style sheet, and as you may have guessed, the output remains the same. Only the implementation has been updated.

    It is said that the proof of the pudding is in the eating, so let's review the code listing closely to understand what's different!

    For starters, I've imported two Perl modules, XML::XSLT and XML::DOM. The latter allows me to create an XML::DOM object which I pass to the XSLT processor for further processing, as seen below.

    // snip

    # create an instance of the DOM object
    my $xml_parser = new XML::DOM::Parser;
    my $xml_dom = eval { $xml_parser->parsefile ($xmlfile) };

    // snip

    After creating a new instance of the DOM parser, I call the parsefile() method. This returns an XML::DOM object containing the XML document passed as input.

    The rest of the code listing is self-explanatory. I pass the XML::DOM object as input to the XSLT processor. However, this change of input does not alter the behavior of the transform() method. It proceeds to apply the XSL transformations from the style sheet to the XML structure (represented by the DOM object) and voila -- I have the required output.



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