如何使用 saxon s9api 进行 XSLT 2.0 转换? XML 个文件到 XML 个文件
How to do XSLT 2.0 transform using saxon s9api? XML file to XML file
我是 XML 的新手,请多多包涵。我需要将一个 xml 文件转换为另一个 xml 文件。它需要 xslt 2.0。我正在使用 saxon 的 s9api。使用他们的文档这是我目前所拥有的:
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();
}
}
}
这个方向对吗?如果是,这实际上是在执行转换,我该如何指定输出 xml.
您可以在转换器上设置目标,例如输出文件。
Serializer out = new Serializer();
out.setOutputProperty(Serializer.Property.METHOD, "html");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File("<filelocation>"));
然后在变压器上设置
transformer.setDestination(out);
在调用 transform() 之前。
trans.transform();
我是 XML 的新手,请多多包涵。我需要将一个 xml 文件转换为另一个 xml 文件。它需要 xslt 2.0。我正在使用 saxon 的 s9api。使用他们的文档这是我目前所拥有的:
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();
}
}
}
这个方向对吗?如果是,这实际上是在执行转换,我该如何指定输出 xml.
您可以在转换器上设置目标,例如输出文件。
Serializer out = new Serializer();
out.setOutputProperty(Serializer.Property.METHOD, "html");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File("<filelocation>"));
然后在变压器上设置
transformer.setDestination(out);
在调用 transform() 之前。
trans.transform();