Tuesday, December 23, 2008

Xalan, Java, XSLTC

What is difference in xalan XSLT processor and and SUN XSLTC compiler ?
They are completely different products. They are both XSLT processors, but they do the same job in very different ways: Xalan (like nearly all other XSLT processors) is in effect a stylesheet interpreter while XSLTC is a stylesheet compiler: it generates Java bytecodes which can be executed directly by the Java VM.

IBM's JDK includes XSLT4J which is based on Xalan. IBM JDK 1.5 it is based on Xalan Java 2.6.0. So there should be no issue moving from Xalan to the IBM JDK.

SUN's JDK 1.5 contains only XSLTC which does not have all of the features of the Xalan interpretive processor. The main differences are in extension support. XSLTC does not support the following extensions:
  • Dynamic EXSLT extensions
  • NodeInfo extension functions
  • SQL library extension
  • PipeDocument extension
  • Evaluate extension
  • Tokenize extension
  • JavaScript extensions
In addition to the above :
  • It does not support all of the XSLT type to Java type conversions that the interpreter supports. You may be able to work around this by adding an explicit cast (e.g. call to the boolean(), number(), etc. function).
  • It does not work well if you return a Boolean object instead of a boolean from a Java extension, or a Double object instead of a double, etc. You can work around this by returning the primitive types instead of objects.
  • It does not provide a default object when the method being called is not static and no object is provided in the extension call. You can work around this by providing the object in the extension call.
It only supports the abbreviated syntax for Java extension calls (see Using the abbreviated syntax for extensions implemented in Java)

So, when you use SUN's JDK 1.5 or later for Transformation on an XSL like this






You will get the following error:

ERROR: 'The first argument to the non-static Java function 'completeTaskOnExit' is not a valid object reference.'
FATAL ERROR: 'Could not compile stylesheet'
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
at Transform.main(Transform.java:29)


If you want to continue to use the Xalan interpreter with SUN's JDK you can use the endorsed standards override mechanism - Create the "endorsed" directory in "...\jre\lib\endorsed" and copy the Xalan 2.7 files into it.

If you are using Xalan 2.8, then extracting "xalan.jar" from 'Xerces-J-tools.2.8.1.zip' and copying it into the 'endorsed' directory solves the problem of not finding 'org.apache.xalan.processor.TransformerFactoryImpl'.

OR you can also force the SUN JDK to use Xalan by setting the System property :

System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");

References:
Ad xslt-sample (not working on Java 1.5 & 1.6) ...
what is difference in xalan XSLT processor and and SUN XSLTC compiler
jdk1.5 and Xalan.jar differences?

4 comments:

Unknown said...

Hi,

We try to implement the set property by using System class it did not work,

System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");

System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");

Can you please suggest us.

yogi said...

Great Rakesh,
I was facing some problem with XSLT when migrating from sun jdk to IBM jdk. Its solved now with ur help.
Thanks a lot.

Yoga

yogi said...
This comment has been removed by the author.
priyanka mehta said...
This comment has been removed by the author.