{
public static void main(String[] args)
throws Exception
{
// Create an XSLT processor, returning an XSLTProcessor interface.
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
// Create a stylesheet using SAX.
// Instantiate a Xerces SAX parser.
SAXParser saxparser = new SAXParser();
// Create an empty StylesheetRoot. The createStylesheetRoot(String baseURI) method is not
// part of the XSLTProcessor interface, so must use the underlying XSLTEngineImpl object.
// The baseURI is for resolving relative URIs. If null is sent, defaults to the current
// directory.
StylesheetRoot stylesheet = ((XSLTEngineImpl)processor).createStylesheetRoot(null);
// Set up a StylesheetHandler (a SAX DocumentHandler) to receive events
// as the Stylesheet is parsed.
StylesheetHandler stylesheetHandler
= new StylesheetHandler((XSLTEngineImpl)processor, stylesheet);
// Set the StylesheetHandler to listen to SAX events from the SAX parser.
saxparser.setDocumentHandler(stylesheetHandler);
// Parse foo.xsl, sending SAX events to stylesheetHandler, which fills in the
// StylesheetRoot object.
saxparser.parse("foo.xsl");
// Do a SAX-driven transform.
// Reset the parser for a new parse.
saxparser.reset();
// Set the processor Stylesheet property, telling the processor which stylesheet to use.
processor.setStylesheet(stylesheet);
// Set the processor to act as a DocumentHandler, receiving SAX events from the
// SAX parser.
saxparser.setDocumentHandler(processor);
// Set the SAX Parser lexical handler to the XSLTProcessor, so the SAX parser
// can handle lexical events in the XML source, such as the occurrence of comment nodes.
saxparser.setProperty("http://xml.org/sax/properties/lexical-handler", processor);
// Set the processor to output the result to the screen.
processor.setOutputStream(System.out);
// Parse foo.xml, sending SAX events to the processor, which sends output
// to a stream.
saxparser.parse("foo.xml");
}