我有一个小问题。我必须使用 XSLT 从 XML 文件生成 HTML 文件。但是 HTML 文件名是由 XML 内容生成的。就我而言,我解决了以下问题:
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;
}
在我的 XSLT 中我这样做:
<!-- 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, '-', ''))" />
[...]
这个解决方案有效,但我问自己它是否经过优化,或者 XSLT 中是否有生成动态输出文件名的技巧?