Can XSLT execute a shell script at the OS level?

a 夏天 提交于 2019-12-01 00:53:26

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

Kai Weber

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!