Can XSLT execute a shell script at the OS level?

2019-02-15 13:54发布

问题:

I have a production flow that combines XSLT and some shell scripts in about 4 steps before it reaches completion. I execute each step manually at the moment.

I'm using Saxon 9 from the command line in Linux. Can I have the xsl's execute shell scripts.. instead of me?

(I know I could achieve the desired result in another way with Ant or Make.. but prefer to stay in XSLT if it is not a huge effort to do so)

Update: I've minimized my shell scripts and sped things up using Xproc. I'm not entirely satisfied with the result, but Xproc has improved life. Kai's suggestion below looks good.. but I have not tried it yet.

回答1:

You can call java.lang.Runtime.exec() in the same way as any other external Java function.



回答2:

I'm not Java savvy either, but I found with Michael Kay's tutorials on the Saxonica website it's doable.

Here's what I did and what's working well for me: In the root element of the XSLT stylesheet I assigned a namespace for the function (in my case I'm using it for unzipping, so I named the prefix unzip, but that could certainly be anything):

xmlns:unzip="java:java.lang.Runtime"

I am defining a variable with a file path for a batch file to be called later. Then I am using

<xsl:result-document href="{$batchFile}" method="text"> ... </result document> 

to create the batch file. (Unzipping could be certainly done with just a command, but I found the batch file version more handy as I needed to combine the unzip-command with some change directory command and other little stuff. And furthermore using a batch file opens up a world of more elaborate tasks that could be called from the XSLT sheet.)

When I need my batch file be executed, I insert an xsl:message like this:

<xsl:message>Executing <xsl:value-of select="unzip:exec(unzip:getRuntime(),concat('cmd /c /y start ',$batchFile))"/></xsl:message>

Hope that helps, best regards, Kai



标签: shell xslt saxon