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.

Introduction

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
$xslt->dispose();

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.

{mospagebreak title=Objects in the mirror}

All examples, thus far, use a XML file as an input source to the XSLT processor. However, this may not always be true. Consider the following example that provides a XML DOM object as input:

# !/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 the DOM object
my $xml_parser = new XML::DOM::Parser;
my $xml_dom = eval { $xml_parser->parsefile ($xmlfile) };

# check for errors …
if ($@) {
        die(“Sorry, Could not parse the XML file,
$xmlfile.n”); }

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

# some more error handling here …
if ($@) {
        die(“Sorry, Could not create an instance of the XSL
Processor using $xslfile.n”);
}

# transform the XML DOM object using the XSL style sheet
eval { $xslt->transform ($xml_dom) };

# … and here
if ($@) {
        die(“Sorry, Could not transform XML file, $xmlfile.n”);
}

# send to output
print $xslt->toString;

# free up some memory
$xml_dom->dispose();

# throws error
# $xslt->dispose();

Once again, I’ve reused the “portfolio.xml” XML document as well as the “portfolio.xsl” XSLT style sheet, and as you may have guessed, the output remains the same. Only the implementation has been updated.

It is said that the proof of the pudding is in the eating, so let’s review the code listing closely to understand what’s different!

For starters, I’ve imported two Perl modules, XML::XSLT and XML::DOM. The latter allows me to create an XML::DOM object which I pass to the XSLT processor for further processing, as seen below.

// snip

# create an instance of the DOM object
my $xml_parser = new XML::DOM::Parser;
my $xml_dom = eval { $xml_parser->parsefile ($xmlfile) };

// snip

After creating a new instance of the DOM parser, I call the parsefile() method. This returns an XML::DOM object containing the XML document passed as input.

The rest of the code listing is self-explanatory. I pass the XML::DOM object as input to the XSLT processor. However, this change of input does not alter the behavior of the transform() method. It proceeds to apply the XSL transformations from the style sheet to the XML structure (represented by the DOM object) and voila — I have the required output.

{mospagebreak title=Different strokes}

This next example demonstrates the capabilities of the XML::XSLT processor more than its functionality. Consider the XML file that I’ve used in the examples thus far:

<?xml version=”1.0″?>
<portfolio name=”The Bull Pit”>
    <stocks>
        <stock symbol=”KO” companyname=”Coca-Cola”>
            <quantity>500</quantity>
            <currenttradedprice>14.00</currenttradedprice>
           
<previoustradedprice>14.00</previoustradedprice>
       
</stock>
       
<stock symbol=”GE” companyname=”General Electric”>
           
<quantity>2400</quantity>
           
<currenttradedprice>25.00</currenttradedprice>
           
<previoustradedprice>24.25</previoustradedprice>
       
</stock>
       
<stock symbol=”APPLE” companyname=”Apple”>
           
<quantity>3500</quantity>
           
<currenttradedprice>35.00</currenttradedprice>
           
<previoustradedprice>38.00</previoustradedprice>
       
</stock>
   
</stocks>
</portfolio>

Let’s assume that, for some quirky reason, I want to prepare a list of stocks in my portfolio. The output XML should look like this:

<stocks>
    <stock>
        <symbol>KO</symbol>
        <companyname>CocaCola</companyname>
     </stock>
    <stock>
        <symbol>GE</symbol>
        <companyname>General Electric</companyname>
    </stock>
    <stock>
        <symbol>APPLE</symbol>
        <companyname>Apple</companyname>
    </stock>
</stocks>

While I can load the XML file in a DOM object and retrieve specific nodes (and/or attributes) of interest, the exercise — take my word for it — will soon spiral into a complex and unmanageable piece of code.

Instead, I’ll show you how to use XSLT to create a new XML structure from an existing one without much sweat. Fortunately, the XML::XSLT processor has support for the XSLT elements (to create XML elements and attributes) to get the job done.

First, here is the XSL style sheet that brings about the transformation:

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”/portfolio/stocks”>
<xsl:element name=”stocks”>
<xsl:for-each select=”stock”>
<xsl:element name=”stock”>
<xsl:element name=”symbol”><xsl:value-of select=”@symbol” /></xsl:element> <xsl:element name=”companyname”><xsl:value-of select=”@companyname” /> </xsl:element> </xsl:element> </xsl:for-each> </xsl:element>
</xsl:template>
</xsl:stylesheet>

A quick word about the XSLT style sheet listed above: note the use of the <xslt:element> XSLT element to define a custom <stocks> element. Next, I’ve used the <xsl:for-each> construct to iterate over the <stock> elements (in the original XML file) and created the <symbol> and <companyname> elements for each stock in my portfolio.

# /usr/bin/perl

# import required modules
use XML::XSLT;
use XML::DOM;

# define local variables
my $xslfile = “portfolio.xsl”;
my $xmlfile = “portfolio.xml”;
my $outputfile = “stocks.xml”;

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

# some more error handling here …
if ($@) {
       
die(“Sorry, Could not create an instance of the XSL
Processor using $xslfile.n”);
}

# transform the XML DOM object using the XSL style sheet
my $xml_dom = eval { $xslt->transform($xmlfile) };

# … here …
if ($@) {
       
die(“Sorry, Could not transform XML file,
$xmlfile.n”); }

# output new XML to file
eval { $xml_dom->printToFile(“stocks.xml”) };

# … and finally, here.
if ($@) {
       
die(“Sorry, Could not create new XML file,
$outputfile.n”); }

# free up memory
$xml_dom->dispose();
$xslt->dispose();

Above, I’ve listed the Perl script that carries out the required transformation. You should notice one minor difference between this example and all other examples that I’ve demonstrated, thus far. Can you spot it? If you drilled straight down to the following code snippet, then pat yourself on the back!

// snip

# transform the XML DOM object using the XSL style sheet
my $xml_dom = eval { $xslt->transform($xmlfile) };

# … here …
if ($@) {
        die(“Sorry, Could not transform XML file, $xmlfile.n”); }

# output new XML to file
eval { $xml_dom->printToFile(“stocks.xml”) };

// snip

Here, you’ll see that I’ve stored the output of the transformation in a DOM object instead of sending it to the output screen. Of course, I’ll need to store the XML data to file eventually. This is where the printToFile() method of the XML::DOM module comes in handy. I pass the name of the output file to this printToFile()method and XML::DOM module will create a file with the required XML output.

{mospagebreak title=Transforming the Transformed}

Nobody’s perfect. And, I am “Nobody.”

Jokes aside, it is likely that the XML data available is not suitable for my requirements. In such situations, I can keep whining about it or rush to author a Perl script to resolve the problem, which is exactly what I have done below. Read on.

Consider the following XML file.

<?xml version=”1.0″?>
<portfolio name=”The Bull Pit”>
    <stocks>
        <stock symbol=”KO” companyname=”Coca-Cola”>
            <quantity>500</quantity>
            <currenttradedprice>14.00</currenttradedprice>
            <previoustradedprice>14.00</previoustradedprice>
        </stock>
        <stock symbol=”GE” companyname=”General Electric”>
            <quantity>2400</quantity>
            <currenttradedprice>25.00</currenttradedprice>
            <previoustradedprice>24.25</previoustradedprice>
        </stock> 
        <stock symbol=”APPLE” companyname=”Apple”>
            <quantity>3500</quantity>
            <currenttradedprice>35.00</currenttradedprice>
            <previoustradedprice>38.00</previoustradedprice>
        </stock> 
    </stocks>
</portfolio>

I’ve already shown you how to convert the above XML to another format that looks something like this:

<stocks>
    <stock>
        <symbol>KO</symbol>
        <companyname>CocaCola</companyname>
    </stock>
    <stock>
        <symbol>GE</symbol>
        <companyname>General Electric</companyname>
    </stock>
    <stock>
        <symbol>APPLE</symbol>
        <companyname>Apple</companyname>
    </stock>
</stocks>

Useful, but remember the XML-phobic stock analyst about whom I referred to in the first part: he would be really happy if the output looked something like this.

Stocks in my portfolio
============================================

Company Name: Coca-Cola

Symbol: KO

——————————————–

… and so on.

As you might have guessed, I’ll require two style sheets in this example. The first one, listed below, has been repeated for the sake of continuity. As I’ve shown you earlier, it creates a new XML structure from an existing one.

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”/portfolio/stocks”>
<xsl:element name=”stocks”>
<xsl:for-each select=”stock”>
<xsl:element name=”stock”>
<xsl:element name=”symbol”><xsl:value-of select=”@symbol” /></xsl:element> <xsl:element name=”companyname”><xsl:value-of select=”@companyname” /></xsl:element> </xsl:element> </xsl:for-
each> </xsl:element>
</xsl:template>
</xsl:stylesheet>

Next, I have another XSL style sheet with instructions to transform the interim XML output into the required analyst-friendly output.

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”/stocks”>
Stocks in my portfolio
<xsl:for-each select=”stock”> ============================================
Company Name: <xsl:value-of select=”companyname” />
Symbol: <xsl:value-of select=”symbol” />
——————————————–
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Finally, here is the Perl script that brings about this back-to-back transformation:

# !/usr/bin/perl

# import required modules
use XML::XSLT;
use XML::DOM;

# define local variables
my $xslfile1 = “portfolio1.xsl”; # for first transformation
my $xslfile2 = “portfolio2.xsl”; # for second transformation
my $xmlfile = “portfolio.xml”;

# check for errors …
if ($@) {
       
die(“Sorry, Could not parse the XML file,
$xmlfile.n”); }

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

# some more error handling here …
if ($@) {
       
die(“Sorry, Could not create an instance of the XSL
Processor using $xslfile1.n”); }

# transform the XML file using the XSL style sheet
# store output as XML DOM object
eval { $xslt->transform($xmlfile) };

# … and here
if ($@) {
       
die(“Sorry, Could not transform XML file,
$xmlfile.n”); }

# store interim XML output in a string variable
$xml_string = $xslt->toString;

# clear up memory
$xslt->dispose();

# reload XSL::XSLT processor with new style sheet
$xslt = eval { XML::XSLT->new ($xslfile2, warnings => 1, debug =>
0) };

# error handling here …
if ($@) {
       
die(“Sorry, Could not create an instance of the XSL
Processor using $xslfile2.n”); }

# transform the XML string in memory using the new XSL style sheet eval { $xslt->transform($xml_string) };

# … and here
if ($@) {
       
die(“Sorry, Could not transform XML::DOM object.
$@n”); }

# send to output
print $xslt->toString;

# free up some memory
$xslt->dispose();

Now, execute this script to view the following output on the screen.

Stocks in my portfolio ============================================

Company Name: Coca-Cola
Symbol: KO

——————————————–

============================================

Company Name: General Electric
Symbol: GE

——————————————–

============================================

Company Name: Apple
Symbol: APPLE

——————————————–

Now it’s time to dissect the script. Initially, I defined an additional variable to store the name of the second style sheet. Next, I instantiated an XML::XSLT object with the first style sheet, transformed the input XML file, and stored the output in the “$xml_string” variable.

At this point, I’ll admit that things didn’t quite go according to plan. Initially, I wanted to demonstrate the open_xsl() method that allows me to load a new style sheet at run-time. Unfortunately, it did not work as advertised; the associated Perl package threw an error that caused my script to stop execution. As I said earlier, nobody’s perfect — and the XML::XSLT module is definitely NOT nobody!

So, time for Plan B. Here, I create another XML::XSLT object, load the second style sheet, pass the “$xml_string” variable as input, transform the interim XML data and render the output — a simple and easy-to-understand listing of all the stocks in the portfolio — on the screen.

Conclusion

This brings me to the conclusion of the two-part series on XSL transformations with Perl.

Let’s quickly review the topics introduced: first, I demonstrated the use of the open_xml() and process() methods; together they provide us with one more mechanism to transform XML documents.

Next, I showed how to transform a XML data structure, available in the form of an XML::DOM object. This comes in handy when you want to perform intermediate transformations and avoid the creation of temporary XML files on the server.

Subsequently, I demonstrated the capabilities of the Perl-based XSLT processor by showing you how to create a new XML data structure from scratch using the <xsl:element> instruction.

The final example demonstrated the concept of transforming dynamic XML created in an earlier XSL transformation. Unfortunately, this example also highlighted the drawbacks of the XML::XSLT module, leaving me with no option but to implement a cheeky workaround to get the job done.

I’ll admit that the XML::XSLT module does not allow a Perl programmer to fully leverage the capabilities of XSL transformations as many features (that are part of the official XSL transformations specification) are not implemented by the module. However, it does provide Perl novices with the required tools to initiate their understanding of XSL transformations in their favorite language.

To summarize, I have listed a couple of URLs for XSL transformations tutorials on the Internet for quick reference:

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

XSL Family at W3C – http://www.w3.org/Style/XSL/

This brings me to end of the series. Till next time, take care. 

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort