Perl Programming Page 4 - XSL Transformations with Perl, Revisited |
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"?> I've already shown you how to convert the above XML to another format that looks something like this: <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"> 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"> Finally, here is the Perl script that brings about this back-to-back transformation: # !/usr/bin/perl # import required modules # define local variables # check for errors ... # create an instance of XSL::XSLT processor # some more error handling here ... # transform the XML file using the XSL style sheet # ... and here # store interim XML output in a string variable # clear up memory # reload XSL::XSLT processor with new style sheet # error handling here ... # transform the XML string in memory using the new XSL style sheet eval { $xslt->transform($xml_string) }; # ... and here # send to output # free up some memory Now, execute this script to view the following output on the screen. Stocks in my portfolio ============================================ Company Name: Coca-Cola -------------------------------------------- ============================================ Company Name: General Electric -------------------------------------------- ============================================ Company Name: 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|