【问题标题】:XSLT 2.0 generate ouput filename from xmlXSLT 2.0 从 xml 生成输出文件名
【发布时间】:2012-09-26 09:42:46
【问题描述】:

我有一个小问题。我必须使用 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 中是否有生成动态输出文件名的技巧?

【问题讨论】:

    标签: java xml xslt-2.0


    【解决方案1】:

    使用 XSLT 2.0,您当然可以根据 XML 输入值创建带有名称和 URL 的结果文档,例如

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

    您也可以从命名模板开始,例如

    <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>
    

    虽然我不确定它是否适合您使用的 JAXP 转换 API。

    【讨论】:

    • 我问 myslef 是否有一个技巧可以轻松地将 XML 文档转换为 HTML 文档,其中 HTML'filename 已从 XML 内容生成。
    • 这不是技巧:Ma​​rtin 向您展示了如何做到这一点。你的代码在做什么我无法理解。 trans.getParameter() 不应以您使用它的方式返回任何内容。
    • @ginogure,我的代码示例创建了一个结果文档,其中的名称是根据将 XPath 表达式 root/foo/bar 评估为具有这些元素和字符串 .xml 的虚构输入文档的结果构造的。因此,调整该路径表达式以适合您的输入文档,并且您有正确的方法。或者发布您的输入示例并告诉我们它的哪一部分应该定义文件名。
    • 好的,抱歉,这正是我想要的。
    【解决方案2】:

    您也可以完全避免使用 for-each。 ...来自工作的 xslt。

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

    这将为找到的每个匹配项生成一个文件。变量 filename-stub 在主模板中计算。不需要 for-each....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 2021-03-21
      • 2020-04-27
      • 2023-03-21
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多