I have a little problem. I have to product a HTML file from a XML file using XSLT. But the HTML filename is generated by XML content.
In my case i solved my problem as following:
public File GenerateHTML(File fileIn) {
File xsltFile = new File(xsltfileString);
File htmlFile = new File(System.getProperty("user.home") + File.separator + "result.html");
File htmlFileFinal = null;
Source xmlSource = new StreamSource(fileIn);
Source xsltSource = new StreamSource(xsltFile);
Result htmlResult = new StreamResult(htmlFile);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans;
try {
trans = transFact.newTransformer(xsltSource);
trans.setParameter("filter_xml", filterXML);
trans.setParameter("FileName", fileIn.getName());
trans.transform(xmlSource, htmlResult);
String outputFileName = (String)trans.getParameter("OutputFilename");
htmlFileFinal = new File(System.getProperty("user.home") + File.separator + outputFileName + ".html");
htmlFile.renameTo(htmlFileFinal);
} catch (TransformerConfigurationException ex) {
LOGGER.log(Level.FATAL, ex.getMessage(), ex);
} catch (TransformerException ex) {
LOGGER.log(Level.FATAL, ex.getMessage(), ex);
}
return htmlFileFinal;
}
and in my XSLT i do :
<!-- general settings -->
<xsl:output method="html" omit-xml-declaration="yes" indent="yes" encoding="UTF-8" />
<xsl:variable name="filter" select="document($filter_xml)/Filtre/Bloc5" />
<!-- transformation body -->
<xsl:template match="*">
<xsl:param name="OutputFilename" select="concat(cac:ContractDocumentReference/cbc:ID, '_', cbc:ID, '_', translate(cbc:IssueDate, '-', ''))" />
[...]
This solution works, but i asked myself if it is optimized or if there is a trick in XSLT to generate a dynamic output filename ?
Well with XSLT 2.0 you can certainly create result documents with names respectively URLs based on XML input values e.g.
You can also start with named template e.g.
although I am not sure how that fits in with the JAXP transformation API you use.