9

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>
4
  • Wich exception did you catch? Commented Mar 29, 2011 at 12:28
  • It is javax.xml.transform.TransformerConfigurationException. I edited my original post, it is there now. Commented Mar 29, 2011 at 12:36
  • Why your last template is empty ? Commented Mar 29, 2011 at 12:41
  • Because that element should be deleted. It finds the element, but doesn't copy it. This XSLT should work, I used it before while I was working in .NET. Commented Mar 29, 2011 at 12:45

3 Answers 3

11

The constructor

public StreamSource(String systemId)

Construct a StreamSource from a URL. I think you're passing the content of the XSLT instead. Try this:

File xslFile = new File("path/to/your/xslt");
TransformerFactory factory = TransformerFactory.newInstance();
Templates xsl = factory.newTemplates(new StreamSource(xslFile));

You must also set the OutputStream that your StreamResult will write to:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result result = new StreamResult(baos);
transformer.transform(xmlInput, result);
return baos.toString();
Sign up to request clarification or add additional context in comments.

5 Comments

Alternatively, just change new StreamSource(XSLTRule) to new StreamSource(new StringReader(XSLTRule))
thanks a lot! but i still have a problem with the Result result = new StreamResult();. What to do there?
@Ivan: what problem do you have with Result result = new StreamResult();?
@Michael For the line transformer.transform I get this exception: javax.xml.transform.TransformerException: Result object passed to ''{0}'' is invalid.I did the same thing for the variable XML as for XSLTRule.
@Ivan: I see: I've edited my answer to address that problem too.
1

You will have to contruct a stream from the xslt string you have and then use it as the stream source

InputStream xslStream = new ByteArrayInputStream(XSLTRule.getBytes("UTF-8"));
Source xslInput = new StreamSource(xslStream);

To get the result to a string :

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Result result = new StreamResult(bos);
    transformer.transform(xmlInput, result);
    String s = new String(bos.toByteArray());
    System.out.println(s);

1 Comment

thanks a lot! but i still have a problem with the Result result = new StreamResult();. What to do there?
0

To use XSLTC, put xalan.jar(2.5), serializer.jar, xml-apis.jar, and xercesImpl.jar on your classpath .

1 Comment

Don't do this. All those packages (from the Xerces project) have been abandoned since 2010. The code now lives in the JVM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.