8.3. XSLTIf you think of XPath as a regular expression syntax, then XSLT is its pattern substitution mechanism. XSLT is an XML-based programming language for describing how to transform one document type into another. You can do some amazing things with XSLT, such as describe how to turn any XML document into HTML or tabulate the sum of figures in an XML-formatted table. In fact, you might not need to write a line of code in Perl or any language. All you really need is an XSLT script and one of the dozens of transformation engines available for processing XSLT.
An XSLT transformation script is itself an XML document. It consists mostly of rules called templates, each of which tells how to treat a specific type of node. A template usually does two things: it describes what to output and defines how processing should continue. Consider the script in Example 8-9. Example 8-9. An XSLT stylesheet<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="html"> <xsl:text>Title: </xsl:text> <xsl:value-of select="head/title"/> <xsl:apply-templates select="body"/> </xsl:template> <xsl:template match="body"> <xsl:apply-templates/> </xsl:template> <xsl:template match="h1 | h2 | h3 | h4"> <xsl:text>Head: </xsl:text> <xsl:value-of select="."/> </xsl:template> <xsl:template match="p | blockquote | li"> <xsl:text>Content: </xsl:text> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> This transformation script converts an HTML document into ASCII with some extra text labels. Each <xsl:template> element is a rule that matches a part of an XML document. Its content consists of instructions to the XSLT processor describing what to output. Directives like <xsl:apply-templates> direct processing to other elements (usually descendants). We won't go into detail about XSLT syntax, as whole books on the subject are available. Our intent here is to show how you can combine XSLT with Perl to do powerful XML munching. You might wonder, "Why do I need to use another language to transform XML when I can do that with the Perl I already know?" True, XSLT doesn't do anything you couldn't do in Perlish coding. Its value comes in the ease of learning the language. You can learn XSLT in few hours, but to do the same things in Perl would take much longer. In our experience writing software for XML, we found it convenient to use XSLT as a configuration file that nonprogrammers could maintain themselves. Thus, instead of viewing XSLT as competition for Perl, think of it more as a complementary technology that you can access through Perl when you need to. How do Perl hackers employ the power of XSLT in their programs? Example 8-10 shows how to perform an XSLT transformation on a document using XML::LibXSLT, Matt Sergeant's interface to the super-fast GNOME library called LibXSLT, one of several XSLT solutions available from your CPAN toolbox.[29]
Example 8-10. A program to run an XSLT transformationuse XML::LibXSLT; use XML::LibXML; # the arguments for this command are stylesheet and source files my( $style_file, @source_files ) = @ARGV; # initialize the parser and XSLT processor my $parser = XML::LibXML->new( ); my $xslt = XML::LibXSLT->new( ); my $stylesheet = $xslt->parse_stylesheet_file( $style_file ); # for each source file: parse, transform, print out result foreach my $file ( @source_files ) { my $source_doc = $parser->parse_file( $source_file ); my $result = $stylesheet->transform( $source_doc ); print $stylesheet->output_string( $result ); } The nice thing about this program is that it parses the stylesheet only once, keeping it in memory for reuse with other source documents. Afterwards, you have the document tree to do further work, if necessary:
The possibilities are endless and, as always in Perl, whatever you want to do, there's more than one way to do it. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|