Saxon process in a java class
Greetings, I'm new to Java and having problems using Dr. Michael Kay's Saxon XSLT processor in a Java class.
Here is my code:
import net.sf.saxon.*;
import net.sf.saxon.TransformerFactoryImpl;
import net.sf.saxon.Transform;
import java.io.*;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.transform.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
public class ApplyStylesheet
{
public static void main(String[] args)
throws javax.xml.transform.TransformerException {
String stylesheet = "<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0' xmlns:xs='http://www.w3.org/2001/XMLSchema'>";
stylesheet = stylesheet + "<xsl:output method='text'/><xsl:template match='docroot'><xsl:value-of select='test_element' />";
stylesheet = stylesheet + "</xsl:template></xsl:transform>";
String xmlsourcedoc = "<?xml version='1.0' encoding='UTF-8'?><docroot><test_element>Hello World!</test_element></docroot>";
String myoutput = apply(stylesheet, xmlsourcedoc);
System.out.println(myoutput);
}
static String apply(String style, String source)
throws javax.xml.transform.TransformerException {
java.lang.System.setProperty("org.xml.sax.parser", "org.apache.crimson.parser.XMLReaderImpl");
java.lang.System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(source);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(style);
javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult();
// javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);
return result.toString();
}
}
And when I execute:
java ApplyStylesheet
I get this error:
Exception in thread "main" javax.xml.transform.TransformerFactoryConfigurationEr
ror: Provider net.sf.saxon.TransformerFactoryImpl could not be instantiated: jav
a.lang.NullPointerException
at javax.xml.transform.TransformerFactory.newInstance(Unknown Source)
at ApplyStylesheet.apply(ApplyStylesheet.java:45)
at ApplyStylesheet.main(ApplyStylesheet.java:27)
Can someone tell me what I am doing wrong please and how to fix it?
Thanks,
Jim Neff