Home arrow Perl Programming arrow XSL Transformations with Perl, Revisited

XSL Transformations with Perl, Revisited

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




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

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 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: