XSL Application


One way to merge and XML document and an XSL stylesheet is through a Java application that you write.  Java provides support for such through its javax.xml packages.  Below is a minimal Java application that reads in three files names, the first two of which are the XML and XSL files, and writes the resulting HTML to the third. 

The program would, of course, be run through the conventional java command in a command prompt window on Windows.  And, the XML packages, in the form of Java JAR files must be in the CLASSPATH, both for compiling and running the application.

 

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class jbsTransform
{

	public static void main(String[] args)
      	throws TransformerException, TransformerConfigurationException, 
    	 	FileNotFoundException, IOException
  	{  

		String xmlFile, xslFile, outFile;
		
		xmlFile = args[0];
		xslFile = args[1];
		outFile = args[2];

		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer(new StreamSource(xslFile));
		transformer.transform(new StreamSource(xmlFile), new StreamResult(new FileOutputStream(outFile)));
	
		System.out.println("Results written to " + outFile);

  	}  // end main
}

It was this program that was used to produce the HTML output shown in the XSL Stylesheet discussion, above.