如何使用任何循环为多个输入指定输入目录? [英] how to specify the input directory using any of the loop for multiple inputs?

查看:108
本文介绍了如何使用任何循环为多个输入指定输入目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MultiXslt
{
    public static void main(String[] args) throws TransformerException,ParserConfigurationException, SAXException, IOException 
    {
        //source xslt
        StreamSource stylesource = new StreamSource("C:/Users/santhanamk/Desktop/newxslt/Xslt inputs/Idml0.xsl");

        DocumentBuilderFactory docbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = docbFactory.newDocumentBuilder();

        //source XML
        Document sourceDoc = dBuilder.parse("C:/Users/santhanamk/Desktop/newxslt/input.xml");

        DOMSource source = new DOMSource(sourceDoc);

        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory
                .newTransformer(stylesource);

        Document document = dBuilder.newDocument();
        DOMResult result = new DOMResult(document);

        transformer.transform(source, result);

        Node resultDoc = ((Document) result.getNode()).getDocumentElement();

        System.out.println(resultDoc.getChildNodes().getLength());

        // print the result
        StringWriter writer = new StringWriter();
        Transformer transformer2 = transformerFactory.newTransformer();
        transformer2.transform(new DOMSource(resultDoc), new StreamResult(writer));
        String s = writer.toString();     

    }
}

实际上我有一个xml文件多个xsl文件(C:/ Users / santhanamk / Desktop / newxslt / Xslt输入/ xsl列表)。当我将xml和xsl0作为输入时,我需要将输出作为字符串。因此,在得到输出后,我需要为xsl1提供与输入字符串相同的输出以获得另一个输出(字符串)。然后我需要将输出作为xsl2的输入字符串来获取另一个输出。当给定的源目录(C:/ Users / santhanamk / Desktop / newxslt / Xslt输入/ xsl列表)doest有任何新的xsl文件加载输出字符串时,它应该给出最终输出为xml!

Actually I have one xml file and multi xsl file (C:/Users/santhanamk/Desktop/newxslt/Xslt inputs/list of xsl). when I give the xml and xsl0 as a input, I need to get the output as a string. So after I got the output,I need to give the same output as a input string for xsl1 to get another output(string). Then I need give the output as a input string for xsl2 to get another output. It should give the final output as xml, when the given source directory (C:/Users/santhanamk/Desktop/newxslt/Xslt inputs/list of xsl) doest have any new xsl file to load the output string!

推荐答案

我认为如果你想链接转换,那么使用JAXP转换API然后 http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT8.html 有一个例子可以做到这一点,使用 XMLFilter 使用 SAXTransformerFactory.newXMLFilter 创建。

I think with the JAXP transformation APIs if you want to chain transformations then http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT8.html has an example to do that, using XMLFilters created with SAXTransformerFactory.newXMLFilter.

此处是一个示例Java代码,它显示了如何使用样式表文件名数组并设置过滤器链:

Here is a sample Java code which shows how to use an array of stylesheet file names and set up a chain of filters:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;


public class JAXPTransChain1 {


    public static void main(String[] args) throws FileNotFoundException, SAXException, ParserConfigurationException, TransformerException {
        String[] stylesheets = new String[] {"sheet1.xsl", "sheet2.xsl", "sheet3.xsl"};
        String inputDoc = "input1.xml";
        chainSheets(stylesheets, inputDoc, new StreamResult(System.out));
    }

    private static void chainSheets(String[] stylesheets, String inputDoc, Result result) throws FileNotFoundException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
        InputSource input = new InputSource(new FileInputStream(inputDoc));

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        XMLReader reader = spf.newSAXParser().getXMLReader();

        SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();

        XMLReader parent = reader;

        for (int i = 0; i < stylesheets.length; i++)
        {
            String sheetUri  = stylesheets[i];
            XMLFilter sheetFilter = stf.newXMLFilter(new StreamSource(new FileInputStream(sheetUri)));
            sheetFilter.setParent(parent);
            parent = sheetFilter;
        }

        Transformer proc = stf.newTransformer();

        SAXSource transSource = new SAXSource(parent, input);

        proc.transform(transSource, result);   

    }

}

如果输入是一个样本

<root>
    <foo>bar</foo>   
</root>

和样式表例如

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/node()[last()]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <xsl:comment>sheet 1</xsl:comment>
    </xsl:template>

</xsl:stylesheet>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/node()[last()]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <xsl:comment>sheet 2</xsl:comment>
    </xsl:template>

</xsl:stylesheet>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/node()[last()]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <xsl:comment>sheet 3</xsl:comment>
    </xsl:template>

</xsl:stylesheet>

然后最终输出是

<?xml version="1.0" encoding="UTF-8"?><root>
    <foo>bar</foo>   
</root><!--sheet 1--><!--sheet 2--><!--sheet 3-->

所以样式表都按输入数组的顺序应用。一旦知道了你想要的订单,就可以很容易地从目录中的文件列表中设置这样的数组。

so the stylesheets have all been applied in the order of the input array. It should be easy to set up such an array from a listing of files in a directory, once you know which order you want.

这篇关于如何使用任何循环为多个输入指定输入目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆