简体   繁体   中英

I am getting exception in XSLT saxon parser

I have added a maven dependency for saxen parser, then sending the xml and xsl as a string parameter to the saxonTransform(String xml, String xsl). But getting some exception.

<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>10.6</version>
</dependency>

SAXON TRANSFORMER(XSLT) CODE:

public static String saxonTransform(String xml, String xsl) throws TransformerException, FileNotFoundException {

String result = "";

System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

StringWriter out = new StringWriter();

TransformerFactory tFactory = TransformerFactory.newInstance();

tFactory.setAttribute(FeatureKeys.DTD_VALIDATION, false);

StreamSource xmlSource = new StreamSource(new StringReader(xml));

StreamSource xslSource = new StreamSource(new StringReader(xsl));

StreamResult xmlResult = new StreamResult(out);

Transformer transformer = tFactory.newTransformer(xslSource);

transformer.transform(xmlSource, xmlResult);

result = out.toString();

return result;

}

But, the given line is not able to transform the xml:

Transformer transformer = tFactory.newTransformer(xslSource);

Giving the error like:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Handler dispatch failed; nested exception is javax.xml.parsers.FactoryConfigurationError:
Provider org.apache.xerces.jaxp.SAXParserFactoryImpl not found] with root cause

In this line

System.setProperty("javax.xml.parsers.SAXParserFactory",
    "org.apache.xerces.jaxp.SAXParserFactoryImpl");

you're saying that you want to use Apache Xerces as your XML parser, but it looks is if it's not available on the classpath.

(Note that for most purposes these days, using the version of Xerces that's built into the JDK works perfectly well. There was a time when the JDK version had some nasty bugs, but that seems to have been fixed a while ago.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM