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: ./xsltJava.sh mystyle.xsl myxml.xml key1=hello


#!/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

6 comments:

  1. I have updated your script
    https://github.com/physikerwelt/xstlprocJ

    ReplyDelete
  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!

    ReplyDelete
  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. com.sun.org.apache.xalan.internal.xsltc.cmdline 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 com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Parser.java:495)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:410)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:336)
    at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:472)
    at com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile.main(Compile.java:146)
    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...



    ReplyDelete
    Replies
    1. It's a long time but FYI, here's a bug about the style issue:
      https://bugzilla.redhat.com/show_bug.cgi?id=1327649

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  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.

    ReplyDelete