0

少し問題があります。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に動的出力ファイル名を生成するためのトリックがあるのか​​を自問しました。

4

2 に答える 2

3

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 とどのように適合するかはわかりません。

于 2012-09-26T10:02:54.187 に答える
0

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 は不要です....

于 2015-03-11T16:40:46.053 に答える