Perl Programming Page 2 - XSL Transformations with Perl, Revisited |
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 # define local variables # create an instance of the DOM object # check for errors ... # create an instance of XSL::XSLT processor # some more error handling here ... # transform the XML DOM object using the XSL style sheet # ... and here # send to output # free up some memory # throws error 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 // 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|