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

Muh*_*edy 4 java xslt

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

请注意,我不需要知道如何通过Java中的XSL转换xml docuemnt.

我需要的是,我有一些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>
Run Code Online (Sandbox Code Playgroud)

然后我需要从java代码调用该模板.如何 ??

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

Bal*_*usC 16

您可以使用此javax.xml.transformer.TransformerAPI.

这是一个基本的启动示例:

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.
}
Run Code Online (Sandbox Code Playgroud)


Kal*_*see 2

下面是一些简单 XSL 转换的代码,以及在 Java 中使用 XSL 的一些技巧。这是另一个示例,其中包含示例 XML 和 XSL。