簡體   English   中英

如何從java代碼調用XSL模板?

[英]How to call XSL template from java code?

如何從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>

然后我需要從java代碼調用該模板。 如何 ??

謝謝All guyz,我做到了,請參閱: 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.
}

下面是一些簡單的XSL轉換代碼 ,以及在Java中使用XSL的一些技巧。 這是另一個示例 ,包含示例XML和XSL。

您的問題是您有一個不需要輸入文檔的XSLT嗎? 然后給Transformer對象一些最小的文檔:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM