You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Georg Seipler <ge...@fht-esslingen.de> on 2000/01/10 11:24:27 UTC

Formatting XML and XSL output ?

Dear apache experts,

I've just recently switched from using ProjectX from SUN and XSL:P from
Keith Visco to Xerces and Xalan. Everything is working fine so far, however
I have one problem:

How do I get an indented (pretty-printed) output ?

The code I use so far looks like:

// combine XML document and XSL stylesheet, output resulting HTML
XSLTProcessor processor = XSLTProcessorFactory.getProcessor(new
org.apache.xalan.xpath.xdom.XercesLiaison());

 XSLTInputSource xmlSource = new XSLTInputSource((Node)xmlDoc);
XSLTInputSource xslSource = new
XSLTInputSource((Node)getXslSheet(documentURI));
Document htmlDoc = new DocumentImpl();
XSLTResultTarget xslt = new XSLTResultTarget(htmlDoc);
processor.process(xmlSource, xslSource, xslt);

 FormatterToHTML fth = new FormatterToHTML(out);
TreeWalker tw = new TreeWalker(fth);
tw.traverse(htmlDoc);

with out being a PrintWriter (of the servlet) and getXslSheet a method to
fetch the sheet from a cache.

It works. But the output is unreadable (everything in one line, or close).
Once more: how do I get an indented, readable output of an existing DOM ?
(preferably without writing my own printer for this)

Anybody able to help ?

Thanks.
And Greetings,
Georg Seipler
(geseit01@fht-esslingen.de)



Re: Formatting XML and XSL output ?

Posted by Georg Seipler <ge...@fht-esslingen.de>.
Hi again ...

Answering my own question (for those with similar problems):

> How do I get an indented (pretty-printed) output ?

Derive from FormatterToXML and modify the settings, such as:

// inner class: formatter for xml with indentation of 2 spaces
class MyFormatterToXML extends FormatterToXML {
  MyFormatterToXML(java.io.Writer writer) {
    super(writer); this.indent = 2; this.m_doIndent = true;
  }
}

This is a design issue, I know, but I'd rather like according set/get
methods for indentation. Perhaps in a future version of xalan ?

Greetings,
Georg Seipler
(geseit01@fht-esslingen.de)