3

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
  • 1
    Not answering my own Q..just adding a bit of an epilogue 9 months later. XPROC turns out to fulfill the need I had above. Commented Dec 9, 2013 at 11:13

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. A creative solution. I'll keep this in mind for future projects.
is this possible with xalan-c++ as well? what would i use instead of java.lang.Runtime.exec()
3

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

2 Comments

Is the answer. Not the one I wanted to hear, because I'm not Java savy. BTW, your XSLT 2.0 book has a prominent spot on my desk lately, I like it..thanks.
is this possible with xalan-c++ as well? what would i use instead of java.lang.Runtime.exec()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.