[jdom-interest] Outputting documents to Xalan

Peter Cox peterc at bjs.com.au
Tue Dec 12 22:31:23 PST 2000

New to JDOM and XML in general, I having been using JDOM to generate XML
documents to process with Xalan.

The FAQ states:

The org.apache.xalan.xslt.XSLTProcessor class takes as input an input
document, an input stylesheet, and a target to output to. The two input
must be in the form of an org.apache.xalan.xslt.XSLTInputSource,
which can be constructed from an InputStream. Lucky for you, the
org.jdom.XMLOutputter class writes a JDOM Document to an
OutputStream! So take your JDOM output, pipe it into your XSLT input
(with a
PipedInputStream and PipedOutputStream), and then simply supply a
stylesheet and output as in any other Java and XML application.

Piped I/O ? :( There must be an easier way.

Luckily there is. (Well it worked for me!)

Xalan's XSLTInputSource takes as an argument a org.w3c.dom.Node which
can be generated using a DOMOutputter 
(Other XSLT processors may have similar abilities)

Code fragment follows...

N.B. MUST specify a XercesLiaison argument to the XSLTProcessorFactory
as per note at:

Document xmlDocument;
XSLTInputSource xslSource;
XSLTResultTarget target;

// initialised elsewhere...

DOMOutputter outputter = new DOMOutputter();

Node node = outputter.output(xmlDocument);
XSLTInputSource xmlSource = new XSLTInputSource(node);

XSLTProcessor processor = XSLTProcessorFactory.getProcessor(
   new org.apache.xalan.xpath.xdom.XercesLiaison());

processor.process(xmlSource, xslSource, target);

Hope this helps,


Peter Cox
peterc at bjs.com.au

More information about the jdom-interest mailing list