2
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();     

    }
}

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!

4
  • so your question is, how you loop through your input xlst files? Commented Mar 7, 2016 at 12:10
  • If you have a directory of XSLT stylesheets, how do you determine the order of the stylesheets to be applied? Commented Mar 7, 2016 at 12:27
  • Actually,I have lot of xsl files in the name of idml0,idml1,idml2,idml3.....idml16 and single xml(input.xml). Commented Mar 7, 2016 at 17:10
  • Now im processing with 1 or 2 xslt files. Commented May 11, 2016 at 6:55

1 Answer 1

2

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.

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

    }

}

If the input is a sample like

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

and the stylesheets do e.g.

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

and

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

and

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

then the final output is

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.