I'm want to transform a XML with XSLT in Java. For that I'm using the javax.xml.transform
package. However, I get the exception javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
. This is the code I'm using:
public static String transform(String XML, String XSLTRule) throws TransformerException {
Source xmlInput = new StreamSource(XML);
Source xslInput = new StreamSource(XSLTRule);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslInput); // this is the line that throws the exception
Result result = new StreamResult();
transformer.transform(xmlInput, result);
return result.toString();
}
Note that I marked the line which throws the exception.
When I enter the method, the value of XSLTRule
is this:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
exclude-result-prefixes='msxsl'
xmlns:ns='http://www.ibm.com/wsla'>
<xsl:strip-space elements='*'/>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='@* | node()'>
<xsl:copy>
<xsl:apply-templates select='@* | node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ns:SLA
/ns:ServiceDefinition
/ns:WSDLSOAPOperation
/ns:SLAParameter[@name='Performance']"/>
</xsl:stylesheet>
To use XSLTC, put xalan.jar(2.5), serializer.jar, xml-apis.jar, and xercesImpl.jar on your classpath .
The constructor
Construct a StreamSource from a URL. I think you're passing the content of the XSLT instead. Try this:
You must also set the
OutputStream
that yourStreamResult
will write to:You will have to contruct a stream from the xslt string you have and then use it as the stream source
To get the result to a string :