Home arrow Perl Programming arrow Page 2 - XSL Transformations with Perl, Revisited

Objects in the mirror - Perl

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.

  1. XSL Transformations with Perl, Revisited
  2. Objects in the mirror
  3. Different strokes
  4. Transforming the Transformed
By: Harish Kamath
Rating: starstarstarstarstar / 12
February 06, 2006

print this article



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

# 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 Programming Articles          >>> More By Harish Kamath

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: