using java to get xsl message

2019-09-03 09:06发布

I'm trying to print the <xsl:message> in a JTextArea using JAXP. My Problem is, that I can't create a saxonica Controller with my transformer and I don't know why because I'm using TransformerFactoryImpl as in some answers is called.

Here is my Java Code:

public static void xslTransform(File xmlFile,File xslFile, JTextArea output){
StreamSource source = new StreamSource(xmlFile);
StreamResult result = new StreamResult(xslFile);
TransformerFactoryImpl tfimpl = new TransformerFactoryImpl();
Transformer transformer = tfimpl.newInstance().newTransformer(new StreamSource(xslFile));
Controller controller = new Controller(transformer);

import for Controller:

net.sf.saxon.Controller;

Hope anybody can help me.

KaFu

标签: java xslt saxon
1条回答
太酷不给撩
2楼-- · 2019-09-03 09:44

There has never been a constructor on the Controller class that took a JAXP Transformer as a parameter, I don't know where you got that idea from.

In releases prior to Saxon 9.6, you can cast the Transformer to a Controller if you want to call methods on the Controller object, that is

Controller controller = (Controller)transformer;

In 9.6, the relationship between Controller and Transformer has changed, because the JAXP API is becoming increasingly unsuitable to take advantage of the facilities becoming available in XSLT 3.0. You can now cast the Transformer to net.sf.saxon.jaxp.TransformerImpl, and from the TransformerImpl you can call getUnderlyingController() to get to the Controller.

But do you really want to do it this way? An alternative would be to do

factory.setAttribute(FeatureKeys.MESSAGE_EMITTER_CLASS, MyMessageEmitter.class)

where MyMessageEmitter is your implementation of Saxon's MessageEmitter inferface.

查看更多
登录 后发表回答