Friday, October 5, 2012

command line XSLT 2.0

I had troubles with with XSLT 2.0 transformations done through a system and found myself lacking a command line XSLT 2.0 tool to debug what's going on. I found saxon but it is a piece of code I can't install everywhere and I'm not sure how well does it work.

Thanks to this post I crafted a script with no external dependencies to output transformed document with first param the XSL, second param the XML to transform and the rest of params are key=value pairs if needed. Hope it helps somebody. UPDATE: xalan also has command line support

example: ./ mystyle.xsl myxml.xml key1=hello


set -e

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

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 mytran.xsl
   echo stylesheet does not exist
   exit 1

if [ -f "$XML" ]; then
   java "$XML" mytran "$@"
   echo no file to transform
   exit 1


  1. I have updated your script

  2. Hello, you seem to be using xalan so I'd say your script is more of a different thing than an update. My script above is using built-in java xslt implementation.

    If you are not allowed to use external dependencies or want to test particularly the java implementation, then you can't use your script. On the other hand if you want to test what xalan does, then your script may help with that.

    Thanks for sharing!

  3. OK. Maybe you can link to some testdata that works with your script.
    So I thought the links to the class-names might be outdated.
    i.e. seems to refer
    to a java 6 version. Does it work with openjdk 1.7?
    With my test data I got a java.lang.NullPointerException
    " at
    Compiler errors:
    Could not compile stylesheet"

    I found out that my approach does not work yet as well, since the function
    "regex-group" seems to be unsupported by xalan...

    1. It's a long time but FYI, here's a bug about the style issue:

  4. This comment has been removed by the author.

  5. Finally I updated to jaxon. That solved my problems. I included the test files to the most recent version of my github repository.
    The drawback of my solution is that it requires maven.