Perl
  Home arrow Perl arrow Page 2 - XSL Transformations with Perl, Revisit...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - 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




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