如何从Java代码调用XSL模板? [英] How to call XSL template from java code?
问题描述
如何从Java代码调用XSL模板?
How to call XSL template from java code ?
请注意,我不需要知道如何用Java中的XSL转换xml文档.
Please note that, I don't need to know how to transform xml docuemnt by XSL in Java.
我确切需要的是,我有一些XSLT文档,其中包含执行某些操作的模板,例如:
What I need exactly is that, I have some XSLT document that contains a template that do something, ex:
<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代码中调用该模板.怎样??
Then I need that template to be called from java code. How to ??
谢谢所有小伙子,我做到了,请参阅: http://m-hewedy.blogspot.com/2009/12/how-to-call-xslt-template-from-your.html
Thanks All guyz, I did it, Please see : http://m-hewedy.blogspot.com/2009/12/how-to-call-xslt-template-from-your.html
推荐答案
You can use the javax.xml.transformer.Transformer
API for this.
这是一个基本的启动示例:
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.
}
这篇关于如何从Java代码调用XSL模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!