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.
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:
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"):
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!
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.
# 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.