Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best XSLT engine for Perl?

I would like to know what of the many XSLT engines out there works well with Perl.

I will use Apache (2.0) and Perl, and I want to obtain PDFs and XHTMLs.

I'm new to this kind of projects so any comment or suggestion will be welcome.

Thanks.


Doing a simple search on Google I found a lot and I suppose that there are to many more.

  • http://www.mod-xslt2.com/
  • http://xml.apache.org/xalan-j/
  • http://saxon.sourceforge.net/
  • http://www.dopscripts.com/xslt_parser.html

Any comment on your experiences will be welcome.

like image 628
David Ameller Avatar asked Oct 01 '08 08:10

David Ameller


3 Answers

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" );
like image 132
Penfold Avatar answered Nov 19 '22 23:11

Penfold


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

like image 33
moritz Avatar answered Nov 19 '22 23:11

moritz


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.

like image 41
aku Avatar answered Nov 19 '22 23:11

aku