How to call an XSL template from java code?

How to call an XSL template from java code?

Please note that I do not need to know how to convert xml documentemnt using XSL in Java.

What I need is that I have an XSLT document containing a template that does something, for example:

<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 this template to call from java code. How?

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

+3
source share
3 answers

You can use the API for this javax.xml.transformer.Transformer.

Here is an example of the main launch:

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.
}
+14
source

XSL, XSL Java. , XML XSL.

+2

Is your question that you have an XSLT that does not require document input? Then just pass some minimal document to the Transformer object:

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

0
source

Source: https://habr.com/ru/post/1725865/


All Articles