XSL Transformation with Perl - My Investment Portfolio (Page 3 of 5 )
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"?>
<portfolio>
<name>The Bull Pit</name>
<stock>
<symbol>KO</symbol>
<companyname>Coca-Cola</companyname>
<quantity>500</quantity>
<lasttradedprice>14.00</lasttradedprice>
</stock>
</portfolio>
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
Stock(s):
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" />
Stock(s):
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" />
</xsl:template>
</xsl:stylesheet>
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:
http://www.w3.org/Style/XSL/
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
$xslt->dispose();
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?
Next: Demystifying the XML::XSLT processor >>
More Perl Articles
More By Harish Kamath