Home arrow Perl Programming arrow Page 4 - XSL Transformation with Perl

Demystifying the XML::XSLT processor - Perl

Perl may not be as well known as some of the other languages, but it boasts a powerful library of packages and modules that everyone can use to work with XML. In this article, Harish Kamath explains how to get started with the "XML::XSLT" package that allows you to transform XML documents by using XSLT style sheets using Perl.

  1. XSL Transformation with Perl
  2. Getting started
  3. My Investment Portfolio
  4. Demystifying the XML::XSLT processor
  5. Error Management
By: Harish Kamath
Rating: starstarstarstarstar / 16
July 25, 2005

print this article



The XML document in the first example listed only one stock in my sample portfolio, but things are a bit more complicated in real life. Consider the next XML file that contains several stocks in a single portfolio -- after all, no one likes to put all their eggs into one basket!

<?xml version="1.0"?>
    <name>The Bull Pit</name>
      <companyname>General Electric</companyname>

Take a look at the accompanying XSLT style sheet. Iíve updated the code to make it modular in its approach. Youíll notice that each element in the XML file has its very own <xsl:template> giving you greater flexibility. Also, note the use of ASCII characters in the "portfolio" and "lasttradedprice" templates to generate a fancy output, as shown later.

<xsl:stylesheet version="1.0" xmlns:xsl="

<xsl:template match="/portfolio">
Portfolio Name: <xsl:value-of select="name" /> 
<xsl:apply-templates select="stock"/>

<xsl:template match="stock">
<xsl:apply-templates select="companyname"/>
<xsl:apply-templates select="symbol"/>
<xsl:apply-templates select="quantity"/>
<xsl:apply-templates select="lasttradedprice"/>

<xsl:template match="companyname">
Company Name:<xsl:value-of select="." />

<xsl:template match="symbol">
Symbol: <xsl:value-of select="." />

<xsl:template match="quantity">
Quantity:<xsl:value-of select="." />

<xsl:template match="lasttradedprice">
Last Traded Price:<xsl:value-of select="." />


Next, I have the glue that brings the two together: the Perl script. Iíve made some minor updates to the script from the first example. I will explain more about these changes after you have reviewed the next code listing and the subsequent output.

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

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

# send to output
print $xslt->toString;

# free up some memory

Ö and the output.

Portfolio Name: The Bull Pit 

Company Name:Coca-Cola
Symbol: KO
Last Traded Price:14.00

Company Name: General Electric
Symbol: GE
Last Traded Price:20.00

Company Name: Apple
Symbol: APPLE
Last Traded Price:14.00

Yes, I have omitted the huge amounts of text that may have scrolled across your screen. The reason for this "strange" behavior will be unraveled in the next few lines as I proceed to de-mystify the updated Perl script.

As you can see above, there are no changes to the first few lines of the script. Next, take a look at the following code snippet:

// snip

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

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

# send to output
print $xslt->toString;

// snip


Note the use of the "warnings" and "debug" properties in the new() method. As the names suggest, these instruct the XSLT processor to output useful -- but verbose, as is evident from the output that scrolled across your screen -- debug information. I have turned on both features by setting the value of the properties to "1", and I recommend that you do the same to help rectify annoying errors, inevitable during the initial stages of any project.

Next, I have opted for the transform() method instead of the serve() method, used earlier. The most significant difference between the two methods is the return value -- the latter, as we have already seen, returns a string that can print()ed on the screen without much fuss. It is not so with the transform() method; this returns an XML DOM object. Donít take my word for it; print the return value and you should see something like this on your screen.


Not pleasant at all. But there is no reason to despair; the XML::XSLT processor is equipped with a handy "toString" property that spits out the result of the transformation in a human-friendly format -- as is evident from the output, listed above.

If youíre wondering why you would want another XML DOM object, the reason  is simple: it is likely that the result of the transformation could serve as the input to another process that is expecting an XML file. In such a case, it would be handy to have a DOM object with the XML structure in memory.

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