I'm new to XML so bear with me. I need to transform an xml file to another xml file. It needs xslt 2.0. I am using saxon's s9api. Using their documentation this what I have so far:
import java.io.File;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;
class Main{
public static void main(String args[]){
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
DocumentBuilder builder = processor.newDocumentBuilder();
try {
builder.build(new File("C:\\XMLFILE.xml"));
} catch (SaxonApiException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
XsltExecutable xsl = compiler.compile(new StreamSource(new File("C:\\XSLFILE.xsl")));
XsltTransformer trans = xsl.load();
trans.transform();
} catch (SaxonApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Is this the right direction? If it is and this is actually performing the transformation how do I specify the output xml.
You can set a destination on the transformer, for example an output File.
Serializer out = new Serializer();
out.setOutputProperty(Serializer.Property.METHOD, "html");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File("<filelocation>"));
Then set on the transformer
transformer.setDestination(out);
Before you make the call to transform().
trans.transform();
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.