简体   繁体   English

如何使用Saxon s9api进行XSLT 2.0转换? XML文件到XML文件

[英]How to do XSLT 2.0 transform using saxon s9api? XML file to XML file

I'm new to XML so bear with me. 我是XML新手,请多多包涵。 I need to transform an xml file to another xml file. 我需要将一个xml文件转换为另一个xml文件。 It needs xslt 2.0. 它需要xslt 2.0。 I am using saxon's s9api. 我正在使用撒克逊人的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. 如果是,并且这实际上在执行转换,那么如何指定输出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(). 在调用transform()之前。

trans.transform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM