What is the best XSLT engine for Perl?

So far I'm very satisfied with XML::LibXML for non-xslt tasks, and its documentation points to XML::LibXSLT, which looks quite nice, but I have no experience with it so far


First mistake - search on CPAN, not Google :)

This throws up a bunch of results, but does rather highlight the problem of CPAN, that there's more than one solution, and it's not always clear which ones work, have been abandoned, are broken, slow or whatever.

And disturbingly, the best answer (or at least, one of the best) comes up on page four of the results :( As other folks have suggested, XML::LibXSLT is robust and does the job:

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

  my $parser = XML::LibXML->new();
  my $xslt = XML::LibXSLT->new();

  my $source = $parser->parse_file('foo.xml');
  my $style_doc = $parser->parse_file('bar.xsl');

  my $stylesheet = $xslt->parse_stylesheet($style_doc);

  my $results = $stylesheet->transform($source);

  print $stylesheet->output_string($results);

If you want to output results to a file then add this

#create output file
open(my $output_xml_file_name, '>', 'test.xml');
print $output_xml_file_name "$results";

If you don't want to do anything fancy, though, there's XML::LibXSLT::Easy, which essentially just wraps the above in one method call (and does a bunch of clever stuff behind the scenes using Moose. Check the source for an education!).

  use XML::LibXSLT::Easy;

  my $p = XML::LibXSLT::Easy->new;

  my $output = $p->process( xml => "foo.xml", xsl => "foo.xsl" );

Can't really say which is the best solution because I didn't have a chance to try them all.
But I can recommend you to try Perl module LibXSLT.
It's an interface to the gnome libxslt library. I used it on one of my recent project was satisfied with it.