[JAVA] [XSLT]如何获取xsl:result-document动态文件名?

时间:2017-06-04 06:36:18

标签: java xml xslt

我使用xsl:result-document来输出具有动态名称的文件,我需要获取文件名才能正常工作,但我不知道如何获取此信息文件名(或路径文件),请帮帮我,谢谢。

下面是我的xslt文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="table">
            <xsl:variable name="var1_row" as="node()" select="row"/>
            <xsl:variable name="var2_let" as="node()">
                <table>
                    <xsl:attribute name="xsi:noNamespaceSchemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance" select="'D:/project/sgb/NetBeansProjects/SGBExport/src/Projects/sgb_soft_mrp/template_export_1.xsd'"/>
                    <row>
                        <xsl:sequence select="($var1_row/@node(), $var1_row/node())"/>
                    </row>
                </table>
            </xsl:variable>
            <xsl:result-document href="{fn:concat(fn:concat('file:///D:/tmp/test/export/002/', fn:string($var1_row/data.management_id)), '.xml')}" encoding="UTF-8">
                <xsl:sequence select="$var2_let"/>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这是我的xml数据:

<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
  <data.management_id>1537</data.management_id>
</row>
</table>

和Java:

import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XML {

    public static void main(String[] args) throws Exception {
        //StreamSource xslSource = new StreamSource(new StringReader(xsl));
        StreamSource xslSource = new StreamSource(new File("D:\\tmp\\test\\export\\002\\data.xslt"));
        StreamSource xmlInSource = new StreamSource(new File("D:\\tmp\\test\\export\\002\\data.xml"));
        Transformer tf = TransformerFactory.newInstance().newTransformer(xslSource);

        StreamResult streamResult = new StreamResult(new ByteArrayOutputStream());

        tf.transform(xmlInSource, streamResult);

        String xmlReq = streamResult.getOutputStream().toString();
        System.out.println("xmlReq: " + xmlReq);

        //StringWriter xmlOutWriter = new StringWriter();
        //tf.transform(xmlInSource, new StreamResult(xmlOutWriter));
        //System.out.println(xmlOutWriter.toString());
    }
}

0 个答案:

没有答案