如何从java代码调用XSL模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1915810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:51:58  来源:igfitidea点击:

How to call XSL template from java code?

javaxslt

提问by Muhammad Hewedy

How to call XSL template from java code ?

如何从 Java 代码调用 XSL 模板?

Please note that, I don't need to know how to transform xml docuemnt by XSL in Java.

请注意,我不需要知道如何在 Java 中通过 XSL 转换 xml 文档。

What I need exactly is that, I have some XSLT document that contains a template that do something, ex:

我真正需要的是,我有一些 XSLT 文档,其中包含一个可以执行某些操作的模板,例如:

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td>.</td>
        <td>.</td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>

Then I need that template to be called from java code. How to ??

然后我需要从 Java 代码中调用该模板。如何 ??

Thanks All guyz, I did it, Please see : http://m-hewedy.blogspot.com/2009/12/how-to-call-xslt-template-from-your.html

谢谢大家,我做到了,请参阅:http: //m-hewedy.blogspot.com/2009/12/how-to-call-xslt-template-from-your.html

回答by Kaleb Brasee

Here's some codefor a simple XSL transform, along with some tips for using XSL in Java. And here's another example, complete with an example XML and XSL.

这里有一些用于简单 XSL 转换的代码,以及在 Java 中使用 XSL 的一些技巧。而这里是另一个例子,完全用一个例子XML和XSL。

回答by BalusC

You can use the javax.xml.transformer.TransformerAPI for this.

您可以javax.xml.transformer.Transformer为此使用API。

Here's a basic kickoff example:

这是一个基本的开球示例:

Source xmlInput = new StreamSource(new File("c:/path/to/input.xml"));
Source xsl = new StreamSource(new File("c:/path/to/file.xsl"));
Result xmlOutput = new StreamResult(new File("c:/path/to/output.xml"));

try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
    transformer.transform(xmlInput, xmlOutput);
} catch (TransformerException e) {
    // Handle.
}

回答by Paul Clapham

Is your question that you have an XSLT which doesn't require an input document? Then just give the Transformer object some kind of minimal document:

您的问题是您有一个不需要输入文档的 XSLT 吗?然后给 Transformer 对象提供某种最小的文档:

transformer.transform(new StreamSource(new StringReader("<empty/>")), yourResult);

转换器.transform(new StreamSource(new StringReader("<empty/>")), yourResult);