Belkast was recently asked to implement an XSLT stylesheet to be used within a NetIQ IDM Connector. Rather than start up a Virtual Machine (VM), we decided to take a different approach: test the XSLT from the command line. Fortunately, it’s not very difficult to do this by using the built in XSLT processor in Java.

Using java com.sun.org.apache.xalan.internal.xsltc.cmdline with Compile and Transform, one can easily convert an XML file from one format to another format using an XSLT stylesheet.

Shown below is a BASH script that allows one to test this out on a Linux machine!

#!/bin/bash

set -e
XSL=`readlink -f "$1"`
XML=`readlink -f "$2"`
shift ; shift
TMPDIR=""

cleanup ()
    {
    set +e
    popd > /dev/null
    [[ "$TMPDIR" ]] && rm -rf "$TMPDIR"
    }

TMPDIR=`mktemp -d`
pushd "$TMPDIR" > /dev/null
trap cleanup EXIT

if [ -f "$XSL" ]; then
    cp "$XSL" mytran.xsl
    java com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile mytran.xsl
else
    echo stylesheet does not exist
    exit 1
fi

if [ -f "$XML" ]; then
    java com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform "$XML" mytran "$@"
else
    echo no file to transform
    exit 1
fi