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

My Investment Portfolio - 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



Now that the installation process is out of the way, it's time to get our hands dirty with some code. Consider the following XML document (say "portfolio.xml") that lists the stocks in a sample investment portfolio:

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

While XML-savvy programmers will appreciate the manner in which the data is hierarchically organized, most market analysts and advisers, who are more at home with the ubiquitous Microsoft Excel, will struggle to make any sense of this document. And unfortunately, it is your job to keep them happy so that you can pay your monthly bills. Hence, the need to find a solution that will help convert the above data to an "analyst-friendly" format.

Consider the following output:

Portfolio Name: The Bull Pit
Company Name: Coca-Cola
Symbol: KO
Quantity: 500
Last Traded Price: 14.00

While this may not be fancy - or even useful - youíll agree that any layman, let alone the stock analyst, will understand that the output is a listing of stocks in a portfolio. So, how would I bring about this transformation? As you might have guessed, the answer is XSL Transformations (or XSLT in short).

Take a peek at the following XSLT style sheet (say "portfolio.xsl"):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
Portfolio Name: <xsl:value-of select="portfolio/name" />
Company Name: <xsl:value-of select="portfolio/stock/companyname" />
Symbol: <xsl:value-of select="portfolio/stock/symbol" />
Quantity: <xsl:value-of select="portfolio/stock/quantity" />
Last Traded Price: <xsl:value-of select="portfolio/stock/lasttradedprice" />

As mentioned earlier, Iíll assume that you are familiar with the nuances of the XSLT language. If not, it might be useful to refresh your memory by visiting some of the URLs listed below. But, before you do, donít forget to bookmark this page and come back once youíre done!

XSLT Tutorial at W3Schools: http://www.w3schools.com/xsl/default.asp

XSLT Tutorial at Zvon.org: http://www.zvon.org/xxl/XSLTutorial/Books/Book1/

XSLT & XPath Tutorial at TopXML.com: http://www.topxml.com/xsl/tutorials/intro/

XSL Family at W3C:

Hello, Perl!

As the title suggests, this article is about XSL Transformations with Perl and so far, I have not yet written a single line of Perl code. It's time to rectify this little anomaly. Take a look at the next code listing: a simple Perl script that uses the aforementioned XML::XSLT package to transform the "portfolio.xml" document using the "portfolio.xsl" style sheet, both of which were listed in the previous section.

# !/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);

# transform XML file and print output
print $xslt->serve($xmlfile);

# free up some memory

Execute the Perl script (say "portfolio.pl") listed above -- Iíll assume that the "portfolio.xml" and "portfolio.xsl" files are in the sample folder as the script -- and you should see the same "analyst-friendly" output listed in the previous section.

It's time to turn our attention to the "portfolio.pl" script! For starters, Iíve imported the "XML::XSLT" module and defined a couple of variables to store the names of the XML and the XSL files. Now, things start to get interesting. First, Iíve created an instance of the XSLT processor by invoking the new() method of the XML::XSLT module. Observe that I have passed the name of the XSL style sheet file as an input parameter to the method -- a mandatory requirement.

Next, I have invoked the serve() method  that does the dirty work of parsing the input XML document, applying the XSLT instructions from the style sheet and finally, print()ing the "transformed" output.

Finally, like all good programmers, I invoke the dispose() method to clear up any memory occupied by the processor object.

Simple and straightforward, wasnít it?

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