Project

Profile

Help

RE: Different result between java and command line when c... ยป SaxonTestMain.java

Andy Walshe, 2018-07-17 17:30

 

import net.sf.saxon.TransformerFactoryImpl;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SaxonTestMain {

private static final TransformerFactory factory = new TransformerFactoryImpl();
private static final ClassLoader cl = Thread.currentThread().getContextClassLoader();

public static void main(String[] args) {
try {
String stylesheet = "call-named-template.xsl";
final File inputFile = new File(ClassLoader.getSystemResource(stylesheet).toURI());

StreamSource streamSource = new StreamSource(cl.getResourceAsStream(stylesheet));
streamSource.setSystemId(inputFile);
Transformer transformer = factory.newTransformer(streamSource);

StringWriter writer = new StringWriter();
final Path inputXmlPath = Paths.get(cl.getSystemResource("simple.xml").toURI());
final String inputXml = new String(Files.readAllBytes(inputXmlPath));

transformer.transform(new StreamSource(new StringReader(inputXml)), new StreamResult(writer));

System.out.println(writer.toString());
} catch (final TransformerException | IOException | URISyntaxException exception) {
System.out.println("Exception caught!");
}
}
}
    (1-1/1)