简体   繁体   English

XSLT 2.0从xml生成输出文件名

[英]XSLT 2.0 generate ouput filename from xml

I have a little problem. 我有一点问题。 I have to product a HTML file from a XML file using XSLT. 我必须使用XSLT从XML文件中生成HTML文件。 But the HTML filename is generated by XML content. 但是HTML文件名是由XML内容生成的。 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 : 在我的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, '-', ''))" />
[...]

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 ? 此解决方案有效,但我问自己是否经过优化或XSLT中是否有技巧来生成动态输出文件名?

Well with XSLT 2.0 you can certainly create result documents with names respectively URLs based on XML input values eg 借助XSLT 2.0,您当然可以基于XML输入值创建名称分别为URL的结果文档,例如

<xsl:template match="/">
  <xsl:result-document href="{root/foo/bar}.xml">
    <xsl:apply-templates/>
  </xsl:result-document>
</xsl:template>

You can also start with named template eg 您也可以从命名模板开始,例如

<xsl:template name="main">
  <xsl:variable name="doc1" select="doc('input.xml')"/>
  <xsl:result-document href="{$doc1/root/foo/bar}.xml">
    <xsl:apply-templates select="$doc1/node()"/>
  </xsl:result-document>
</xsl:template>

although I am not sure how that fits in with the JAXP transformation API you use. 尽管我不确定您所使用的JAXP转换API是否适合它。

You can also avoid the for-each altogether. 您也可以完全避免使用for-each。 ... from a working xslt. ...从有效的xslt中获取。

<xsl:template match="EXTRACT-DATASETS/SUBSET">
...
...
<xsl:result-document href="{$filename-stub}.ctl">
...
...
</xsl:result-document>
</xsl:template>

This will generate a file for every match it finds. 这将为找到的每个匹配项生成一个文件。 The variable filename-stub is calculated within the main template. 变量filename-stub在主模板中计算。 No for-each needed.... 不需要每个...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM