I am trying to perform a simple xml transformation using XSLT 1.0. Here are my xml and xslt files.
XML File
Somehow you are executing your XSLT file as Perl code, but there is nothing in your question to explain how. In fact, as I commented, the Perl code that you show cannot have caused the error you say it did because it won't compile
I can see a problem with the call to $stylesheet->transform_file($source)
, which should be either $stylesheet->transform($source)
or $stylesheet->transform_file($xmlfile)
, but the rest of the bugs are obvious
Note also that the stylesheet attached to the XML document with the xml-stylesheet
processing instruction is test.xsl
, whereas your Perl code applies test.xslt
. You should choose one or the other
Your call to $stylesheet->output_as_bytes($results)
is better as $stylesheet->output_as_chars($results)
. It doesn't make any difference with pure ASCII data, but the former will produce encoded octets, which is rarely useful. Usually you just want a character string
It's best to avoid writing fancy parameter input and exception-handling code before you have the basic program working. I suggest you start from my code here instead, and use the Try::Tiny module instead of a simple eval
if you must handle the errors. At present, all your handlers seem to do is supplement the exception message with a lot of stars and then die anyway, so I think you can do without them
use strict;
use warnings;
use XML::LibXSLT;
my ($xmlfile, $xsltfile) = qw/ example.xml trans.xsl /;
my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $results = $stylesheet->transform_file($xmlfile);
print $stylesheet->output_as_chars($results);
Article - My Article
Authors:
- Mr. Foo
- Mr. Bar
Execution of trans.xslt aborted due to compilation errors.
This looks like you're trying to execute your XSLT file, not your Perl program.
You should be running something like this:
$ ./your_xslt_processor.pl -f your_xml.xml -p trans.xslt