XML technologies / XSLT / XSLT Cookbook / Extending and embedding XSLT / XSLT in Perl: usage

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

Your problem has a suitable Perl solution, but a little XSLT would make it easier.

Solution

There are several ways to embed XSLT in Perl. XML::LibXSLT and XML::LibXML are Perl frontends for the functionality of the SAX and XSLT processors in the GNOME library. Borrowed from Perl & XML by Erik T. Ray and Jason McIntosh (O’Reilly, 2002), this example batch-processes several XML files with a once-compiled XSLT script:

use XML::LibXSLT;
use XML::LibXML;

# die Argumente dieses Befehls sind Stylesheet- und Quelldateien
my( $style_file, @source_files ) = @ARGV;

# initialisiere den Parser und XSLT-Prozessor
my $parser = XML::LibXML->new( );
my $xslt = XML::LibXSLT->new( );
my $stylesheet = $xslt->parse_stylesheet_file( $style_file );

# für jede Quelldatei: Parse, transformiere, gib Ergebnis aus
foreach my $file ( @source_files ) {
  my $source_doc = $parser->parse_file( $source_file );
  my $result = $stylesheet->transform( $source_doc );
  print $stylesheet->output_string( $result );
}

Stylesheet parameters can be passed as a Perl hash:

# Ähnlicher Code aus vorherigem Beispiel wurde weggelassen.

my %params = {
     param1 => 10,
    param2 => 'foo',
} ;
foreach my $file ( @source_files ) {
  my $source_doc = $parser->parse_file( $file );
  my $result = $stylesheet->transform($source_doc, %params);
  print $stylesheet->output_string( $result );
}

Passing parameters from Perl would, for example, let a Perl CGI program accept HTML form input and query an XML database using XSLT; see recipe Serving SOAP documentation from WSDL, where we cheated slightly by forking rather than embedding the processor.

XML::Xalan is another Perl XSLT module invoking processor Xalan. Its author, Edwin Pratomo, still considers it alpha software.

The simplest way to use XML::Xalan is through external files:

use XML::Xalan;

  # konstruiere den Transformer
  my $tr = new XML::Xalan::Transformer;

  # kompiliere das Stylesheet
  my $compiled = $tr->compile_stylesheet_file("my.xsl");

  # parse das Eingabequelldokument
  my $parsed = $tr->parse_file("my.xml");

  my $dest_file = "myresult.xml" ;

  # führe die Transformation aus, und speichere das Ergebnis
  $tr->transform_to_file($parsed, $compiled, $dest_file)
    or die $tr->errstr;

Returning the result in a variable for further processing is more useful:

 my $res = $tr->transform_to_data($parsed, $compiled); 

You need not parse input or precompile the stylesheet; both can be passed as files or strings:

 my $res = $tr->transform_to_data($src_file, $xsl_file); 

This returns a literal string, making most sense for text output you want to process further in Perl.

Alternatively, access results through events:

# erzeuge einen Handler sub
$out_handler = sub {
     my ($ctx, $mesg) = @_;
     print $ctx $mesg;
 };
# führe die Transformation über den Handler aus
$tr->transform_to_handler(
     $xmlfile, $xslfile,
     *STDERR, $out_handler);

Discussion

Many Perl developers have not fully embraced XSLT because, once they master Perl, solving anything without it becomes difficult. Fairly, most recognize other languages’ merits. XSLT can certainly simplify complex XML transformations even when most surrounding code remains pure Perl.

See also

Another Perl XSLT solution is XML::GNOME::XSLT by T. J. Mather, a Perl frontend to libXSLT, GNOME’s C-based XSLT processor. You can also use XML::XSLT, Jonathan Stowe’s native Perl implementation. It still lacks many advanced XSLT 1.0 features, including xsl:sort, xsl:key and xsl:import, with only partial support elsewhere. A third option is XML::Sablotron by Pavel Hlavnicka, a Perl frontend to Ginger Alliance’s C++ XSLT product. More information is at CPAN-org.

Finally, another Perl XSLT solution is Apache AxKit, an XML application server for Apache. AxKit uses a pipeline processing model, handling content in stages. Its XSLT functionality comes from the Sablotron processor.

  

 

 

 

back to top