Java中的Saxon:XSLT用于CSV到XML [英] Saxon in Java: XSLT for CSV to XML

查看:225
本文介绍了Java中的Saxon:XSLT用于CSV到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大部分从此问题继续: XSLT:CSV (或平面文件或纯文本)转换为XML

Mostly continued from this question: XSLT: CSV (or Flat File, or Plain Text) to XML

所以,我有一个XSLT从这里: http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html

So, I have an XSLT from here: http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html

并将CSV文件转换为XML文档。它在命令行中使用以下命令时执行此操作:

And it converts a CSV file to an XML document. It does this when used with the following command on the command line:


java -jar saxon9he.jar -xsl:csv-to-xml .csv -it:main -o:output.xml

java -jar saxon9he.jar -xsl:csv-to-xml.csv -it:main -o:output.xml

现在问题变成了:这在我的Java代码?

现在我有如下代码:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
StreamSource xsltSource = new StreamSource(new File("location/of/csv-to-xml.xsl"));
Transformer transformer = transformerFactory.newTransformer(xsltSource);
StringWriter stringWriter = new StringWriter();
transformer.transform(documentSource, new StreamResult(stringWriter));
String transformedDocument = stringWriter.toString().trim();

Transformer code> net.sf.saxon.Controller 。)

(The Transformer is an instance of net.sf.saxon.Controller.)

命令行上的诀窍是指定-it:main 直接指向XSLT中的命名模板。这意味着你不必为源文件提供-s标志。

The trick on the command line is to specify "-it:main" to point right at the named template in the XSLT. This means you don't have to provide the source file with the "-s" flag.

问题再次从Java端开始。在哪里/我如何指定这个-it:main?不会这样做打破其他XSLT的不需要指定?我需要命名每个模板在每个XSLT文件main?给定Transformer.transform()的方法签名,我指定源文件,所以不会破坏我在做这个东西的所有进展?

The problem starts again on the Java side. Where/how would I specify this "-it:main"? Wouldn't doing so break other XSLT's that don't need that specified? Would I have to name every template in every XSLT file "main?" Given the method signature of Transformer.transform(), I have to specify the source file, so doesn't that defeat all the progress I've made in figuring this thing out?

Edit:我发现s9api隐藏在saxon9he.jar里面,如果有人在寻找它。

I found the s9api hidden inside the saxon9he.jar, if anyone is looking for it.

推荐答案

您正在使用为XSLT 1.0设计的JAXP API。如果你想使用XSLT 2.0的功能,像在命名模板上启动转换的能力,我建议使用s9api接口,这是更好的设计为这个目的。

You are using the JAXP API, which was designed for XSLT 1.0. If you want to make use of XSLT 2.0 features, like the ability to start a transformation at a named template, I would recommend using the s9api interface instead, which is much better designed for this purpose.

然而,如果你有很多现有的JAXP代码,并且你不想重写它,你通常可以通过将JAXP对象向下转换到底层的Saxon实现类来实现你想要的。例如,您可以将JAXP Transformer转换为net.sf.saxon.Controller,并允许您访问controller.setInitialTemplate();当调用transform()方法时,只需要提供null作为Source参数。

However, if you've got a lot of existing JAXP code and you don't want to rewrite it, you can usually achieve what you want by downcasting the JAXP objects to the underlying Saxon implementation classes. For example, you can cast the JAXP Transformer as net.sf.saxon.Controller, and that gives you access to controller.setInitialTemplate(); when it comes to calling the transform() method, just supply null as the Source parameter.

顺便说一下,如果你编写的代码需要2.0处理器,不使用TransformerFactory.newInstance(),它将给你在类路径上找到的任何旧的XSLT处理器。使用新的net.sf.saxon.TransformerFactoryImpl()代替,它(a)更健壮,(b)快得多。

Incidentally, if you're writing code that requires a 2.0 processor then I wouldn't use TransformerFactory.newInstance(), which will give you any old XSLT processor that it finds on the classpath. Use new net.sf.saxon.TransformerFactoryImpl() instead, which (a) is more robust, and (b) much much faster.

这篇关于Java中的Saxon:XSLT用于CSV到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆