Perl Programming XSL Transformations with Perl, Revisited |
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 # define local variables # create an instance of XSL::XSLT processor # error handling here ... # load XML # .. here ... # transform XML file # .. and here. # send to output # free up some memory 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 # .. here ... # transform XML file # .. and here. // 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|