XML technologies / XML / Perl & XML / Working with trees: XPath, XSLT and more / XSLT
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
If you think of XPath as a syntax for regular expressions, XSLT is the analogue of replacing patterns. XSLT is a programming language itself based on XML, describing transformation from an input document to an output document. XSLT can do remarkable things, such as convert XML to HTML or sum table columns. Sometimes it even eliminates the need to write a single line of Perl. All you need is XSLT and one of the many available transformation engines.
An XSLT transformation script is itself an XML document. It consists mainly of rules called templates, each describing how to process a particular node type. A template has two jobs: describing part of the output and describing further document processing.
The origins of XSLT:
XSLT stands for “Extensible Stylesheet Language[:] Transformations.” The name indicates that it is the component of Extensible Stylesheet Language (XSL) responsible for converting an input XML document. The other component is XSL-FO (FO means “Formatting Objects”), a highly presentation-oriented XML variant. An XSL-FO document usually contains the original input content plus very detailed instructions for its presentation.
Although its name ties XSLT to XSL, it can do much more than formatting for final output. It is an important XML tool that makes converting one XML document to another fairly easy. For that reason, the W3C adopted its recommendation early, several years before the rest of XSL was ready.
The specification and links to XSLT tutorials are available on the W3C home page.
Let us examine the script in the following example.
Example: An XSLT stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="html">
<xsl:text>Titel: </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>Kopfzeile: </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="p | blockquote | li">
<xsl:text>Inhalt: </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet> The transformation script reads HTML and converts it to ASCII text with extra markers. Each <xsl:template> element is a rule matching part of the XML document. Its content instructs the XSLT processor about text to output. Instructions such as <xsl:apply-templates> pass control to other elements and their templates, usually child elements. We will not cover XSLT syntax in detail: entire books address it. What matters here is combining XSLT and Perl to bring both technologies’ power to XML processing.
You may ask why you should use another language when Perl can do the same. XSLT indeed adds no fundamentally new capabilities. Its value lies in simplicity and specific purpose. Its basics can be learned in a few hours. In that time, even an experienced Perl programmer might develop only the equivalent of one complex template that takes minutes in XSLT. Our XML software experience suggests XSLT is especially useful when non-programmers will maintain a file later. Treat XSLT as a valuable complement to Perl, usable from Perl whenever you choose, rather than as a competitor.
How does a Perl hacker incorporate XSLT into a program? The next example shows a document transformation with XML::LibXSLT. This module by Matt Sergeant interfaces to the extremely fast GNOME LibXSLT library and is one of several XSLT solutions in the CPAN toolbox.
Note: Other modules include
XML::XSLT, written in Perl, orXML::Sablotron, based on the C libraries Expat and Sablotron (the latter is an XSLT library from the Ginger Alliance).
Example: A program for performing an XSLT transformation
use XML::LibXSLT;
use XML::LibXML;
# Kommandozeilenargumente sind das Stylesheet und die Eingabedatei
my( $style_file, @source_files ) = @ARGV;
# Initialisierung des Parsers und des XSLT-Prozessors
my $parser = XML::LibXML->new( );
my $xslt = XML::LibXSLT->new( );
my $stylesheet = $xslt->parse_stylesheet_file( $style_file );
# Für alle Eingabedateien: parsen, transformieren und Ergebnis ausgeben
foreach my $file ( @source_files ) {
my $source_doc = $parser->parse_file( $source_file );
my $result = $stylesheet->transform( $source_doc );
print $stylesheet->output_string( $result );
}Note that the program reads the stylesheet only once. It then remains in memory for repeated use. This also allows you to adapt the stylesheet and input document to your needs:
Preprocess or postprocess text with search-and-replace functions.
Restrict transformation to part of the document.
Apply an iterator to the tree to give some nodes special treatment that would be cumbersome in XSLT.
The possibilities are endless, and Perl’s motto applies as always: There’s more than one way to do it (TMTOWTDI).
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top