XSLTProc - commandline XSLT processor

Motivation: A portable command line tool capable of performing XSL transformations may always come handy...

More often than not I am required to work on environments where I cannot make any assumptions about the existence of command line utilities. Installing binaries is mostly out of scope in such scenarios. In case the target environment includes a Java Runtime Environment, this almost-one-liner enables you to quickly run XSLT in a platform-agnostic manner.

The solution is neither beautiful, nor exotic, but in return it is short, effective and platform independent. In case you need something more flexible, more tweakable, you might consider using xsltproc in linux/unix environment which is based on GNOME libxslt. In tiny-soft environment msxsl.exe is the freely downloadable component that exposes the MSXML 4.0 XSLT engine to the command line.


package hu.lithium.utils;

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XSLTProc {
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			System.err.println("Usage: java -jar xsltproc.jar <xsltfile> <xmlfile>");
			System.exit(1);
		}
		Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(args[0])));
		trans.transform(new StreamSource(new File(args[1])), new StreamResult(System.out));
	}
}
© 2003-2020 lithium.io7.org
Content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.