Re: [xsl] Calling Java Xalan inside Perl on Unix

Subject: Re: [xsl] Calling Java Xalan inside Perl on Unix
From: David McKain <dmckain1@xxxxxxxxxxx>
Date: Thu, 17 Apr 2003 16:42:13 +0100
On Thu, Apr 17, 2003 at 10:07:42AM -0400, Gan Uesli Starling wrote:
> 
> Anybody know how to call Xalan from a Perl script on
> NetBSD Unix? I can't seem to get it right. I try like
> so in my Perl script...
>
Hi Gan,

> @xsl = ("java", "org.apache.xalan.xslt.Process -in 
> $file_name.xml -xsl pdfmarks.xsl -out pdfmarks.txt");
> system(@xsl);

Your problem is shell escaping. The list @xsl you pass to system() has 2
arguments so is equivalent to typing:

java "org.apache.xalan.xslt.Process -in blah.xml blah..."

at the shell (I've omitted the last part of the command to make it fit
on one line!)

Everything inside the double quotes is treated as ONE argument by the
shell, so what this does is ask Java to find and run the class called
org.apache.xalan.xslt.Process -in blah.xml blah... This class
name includes the space and the '-in' stuff and almost certainly doesn't
exist, hence the error.

To fix, do:

my @xsl = ("java", "org.apache.xalan.xslt.Process", "-in",
            "$filename.xml", ...);
system(@xsl);

Each member of the list @xsl represents a separate command line argument,
which is what you want here.

This particular example can also be written in a less noisy but entirely
equivalent way using Perl's "quote words" qw(...) quoting as:

my @xsl = qw(java org.apache.xalan.xslt.Process -in $filename.xml ...)

Alternatively, use the less safe 'system COMMAND' method:

system("java org.apache.xalan.xslt.Process -in ...");

though do bear in mind that you must be *very* careful about using this
last method if any of the strings being interpolated into the system
command are not hardcoded into your program, as otherwise you may end up
executing

java org.apache.xalan.xslt.Process ; rm -rf /

which probably doesn't do what you want!

For more info, perldoc -f system

You might also want to try looking at either XML::LibXSLT or Sablotron
for XSLT processing in Perl; calling java in this way is not terribly
efficient.

Cheers,
David.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread