user1030620 user1030620 - 13 days ago 5
Java Question

JAVA Transformation returns XSLT instead of transformation result

I try to use Java Transformer with org.w3c.dom.Document for input, output and the XSLT. Unfortunately the output is equal to the XSLT input. So it seems the transformation isn't used correctly. I wasn't able to find any reason for this behavior, so I ask it here and hope for help.

My Code:


import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLTransform {
public static void main(String[] args) {

StringWriter stringWriter = null;
try {
// Read Input Files
Document xslt = parseXML(readFile("...")); // link to XSLT
Document input = parseXML(readFile("...")); // link to input

// Create new Transformer with the XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new DOMSource(xslt));

// create variables for the output
stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);

// transform
transformer.transform(new DOMSource(input), streamResult);

// parse it to XML DOM File
Document resultXML = parseXML(stringWriter.toString());

// output, so we see it is wrong
} catch (Exception e) {
} finally {
try {
if (stringWriter != null) stringWriter.close();
} catch (Exception e1) { };

//Helper-Methods which seems to work//

public static String convertDocumentToString(Document doc) throws TransformerFactoryConfigurationError,
TransformerException {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(doc), new StreamResult(out));
return out.toString();

private static Document parseXML(String str) throws ParserConfigurationException, SAXException, IOException,
SQLException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(str)));

private static String readFile(String filename) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
line = br.readLine();
return sb.toString();
} finally {

And I use following input files:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="/Test/Content"/>

<xsl:template match="/Test/Content">
<xsl:copy-of select="." />


<?xml version="1.0" encoding="UTF-8" standalone="no"?>

And I get the strange output (equal to the XSLT)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="" version="2.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:apply-templates select="/Test/Content"/>
<xsl:template match="/Test/Content">
<xsl:copy-of select="."/>

Does anybody know, why the output is equal to the XSLT-File, and how to fix it? So that it returns the correct transformed XML instead?



Make sure you do


when working with XSLT (both stylesheets as well as input documents).