gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:04:35 25 4
gpt4 key购买 nike

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

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

我真正需要的是,我有一些 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>

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

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

最佳答案

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

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

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.
}

关于java - 如何从 java 代码调用 XSL 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1915810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com