Saturday, February 11, 2012

Xalan install recipe: Perform XSLT transformations from command line

Doing ad-hoc XSLT transformations is a quick and agile way to test your XSL output. Every time I have to show a quick transformation to a developer I have to first download xalan and dependencies locally, type a long java command, build a script so the developer is abstracted from java command line stuff like having to specify each jar file etc.

I keep on finding myself writing Plain Old Bash (POB) recipes for this kind of stuff (automate the installation of tools)

Here is a recipe to get xalan installed and ready to be used just calling a command and passing the XML and XSL from the command line:

#!/bin/bash
#
# xalan-install.sh
#
# 1. Copy this recipe to a directory where you wish to install xalan. 
# 2. Run it and it will install XALAN_JAVA executable inside INSTAL_DIR
# 
#
# @author: Nestor Urquiza
# @date: 20110211
#
#

echo "--- Install Xalan XSLT Standalone processor ---"
 
set -e

XALAN_JAVA=xalan
INSTAL_DIR=xalan-processor
 
mkdir -p $INSTAL_DIR
cd $INSTAL_DIR
curl -O http://mirrors.ibiblio.org/pub/mirrors/maven2/xalan/xalan/2.7.1/xalan-2.7.1.jar
curl -O http://mirrors.ibiblio.org/pub/mirrors/maven2/xalan/serializer/2.7.1/serializer-2.7.1.jar
curl -O http://mirrors.ibiblio.org/pub/mirrors/maven2/xerces/xercesImpl/2.9.0/xercesImpl-2.9.0.jar
echo "#!/bin/bash" > $XALAN_JAVA
echo "set -e" >> $XALAN_JAVA
echo "USAGE=\"Usage: $XALAN_JAVA <path to xml> <path to xsl>\"" >> $XALAN_JAVA
echo "if [ \$# -ne \"2\" ]; then echo \$USAGE; exit 1; fi" >> $XALAN_JAVA
echo "CP=\"`find \".\" -name '*.jar' | xargs echo | tr ' ' ':'`\"" >> $XALAN_JAVA
echo "CLASSPATH=\$CP:\$CLASSPATH" >> $XALAN_JAVA
echo "export CLASSPATH" >> $XALAN_JAVA
echo "java org.apache.xalan.xslt.Process -IN \$1 -XSL \$2" >> $XALAN_JAVA
chmod +x $XALAN_JAVA
echo "$XALAN_JAVA was installed in $INSTAL_DIR directory. Run this to start using xalan: cd $INSTAL_DIR; ./$XALAN_JAVA"
It should be straightforward all you need to do to start using xalan is to navigate to the installation folder and run your transformation for example:
$ cd xalan-processor
$ ./xalan ~/Downloads/portfolio-struct.xml ~/Downloads/portfolio-struct.xsl

Not sure if I will need to change this script down the road but most likely latest version should be available from my svn repo.

No comments:

Followers