vignesh
vignesh

Reputation: 1599

Need to use saxon for transforming html to xml using xslt

I am converting html to xml using xslt1.0. I want to migrate to xslt2.0 for some built-in functions. Currently my transformation code is like,

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

and method for conversion is

public static String convert(String inputHtml, String xsl) throws Exception {
        File xsltFile = new File(xsl);

        InputStream is = new ByteArrayInputStream(inputHtml.getBytes("UTF-8"));
        javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(is);
        javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
        StringWriter sw = new StringWriter();

        javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(sw);

        javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
        javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);

        trans.transform(xmlSource, result);
        return sw.getBuffer().toString();
    }

How to write for saxon processor?. Thanks in advance

Upvotes: 1

Views: 2696

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

The Java code doesn't need to change: if Saxon9.3 is on the classpath, it should load it and use it automatically. But if you want to be 100% confident that Saxon gets loaded (which is probably a good idea if the code uses XSLT 2.0 or Saxon extensions) then change the line

TransformerFactory transFact javax.xml.transform.TransformerFactory.newInstance();

to

TransformerFactory transFact = new net.sf.saxon.TransformerFactoryImpl();

Upvotes: 1

Related Questions