I am trying to perform a simple xml transformation using XSLT 1.0. Here are my xml and xslt files.
XML File
<?xml version="1.0"?>
<?xml-stylesheet type="xsl" href="trans.xsl"?>
<Article>
<Title>My Article</Title>
<Authors>
<Author>Mr. Foo</Author>
<Author>Mr. Bar</Author>
</Authors>
<Body>This is my article text.</Body>
</Article>
XSLT File
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
Article - <xsl:value-of select="/Article/Title"/>
Authors: <xsl:apply-templates select="/Article/Authors/Author"/>
</xsl:template>
<xsl:template match="Author">
- <xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
And here is my perl script that I am using.
use strict;
use warnings;
use Getopt::Std;
use File::Path;
use File::Spec;
use File::Basename;
use Env;
use XML::LibXSLT;
use XML::LibXML;
my %opts = ();
getopts('p:f:'\%opts);
my $xsltfile = $opts{'p'};
die "XSLT file not specified" if !defined($xsltfile);
my $xmlfile = $opts{'f'};
die "XML file not specified" if !defined($xmlfile);
# XSLT Transformation code starts here
#my $xml_parser = XML::LibXML->new();
#my $source = $xml_parser->parse_file($msgcatfile);
my $source = XML::LibXML->load_xml(location => $xmlfile);
#my $xslt_parser = XML::LibXML->new();
#my $xslt_source = $xslt_parser->parse_file($xsltfile);
my $xslt_source = XML::LibXML->load_xml(location => $xsltfile);
my $xslt = XML::LibXSLT->new();
my $stylesheet;
eval { $stylesheet = $xslt->parse_stylesheet($xslt_source); };
if ($@)
{
print "$@";
die "\n!******************Error in parsing the stylesheet file : $xsltfile ************************!\n";
}
eval { my $results = $stylesheet->transform_file($source); };
if ($@)
{
print "$@";
die "\n!******************Error in transforming the input xml file : $source ************************!\n";
}
print $stylesheet->output_as_bytes($results);
0;
I am not sure what is going wrong but when run this perl script, I am getting following errors which I am not able decipher.
Bareword found where operator expected at trans.xslt line 2, near ""1.0" xmlns"
(Missing operator before xmlns?)
Bareword found where operator expected at trans.xslt line 11, near "</xsl"
(Might be a runaway multi-line // string starting on line 10)
(Missing operator before l?)
syntax error at trans.xslt line 2, near "xsl:"
Execution of trans.xslt aborted due to compilation errors.
I could not find any similar posts (relevant to XML/XSLT) when I searched for keywords in the error message. Am I missing something obvious?
:UPDATE:
I ran my program as
perl transform.pl -p trans.xslt -f example.xml