Re: [xsl] Optimizing Trax

Subject: Re: [xsl] Optimizing Trax
From: "Mark R. Diggory" <mdiggory@xxxxxxxxxxxxxxxxx>
Date: Tue, 24 Jun 2003 09:26:45 -0400
Pietschmann is quite right here about DOM memory usage, but, even if you use SAXTransformer, SAXSource and SAXResult, I still believe there is a DOM tree generated internal to the transformer to handle the walking the tree on various axis. I sure different implementations approach dealing with this with different optimizations. "Sources" and "Results" really only provide alternate means of IO and not underlying DOM creation.

There is a chance that instantiating the DOM and handing it as a source may be more memory consuming than letting the Transformer build it, but this would be very implementation specific.

One alternative is to use a SAX XMLFilter to process out content you may not need prior to doing your transform, a tool I've just started to use http://www.devsphere.com/xml/saxdomix/index.html for transforming/filter processing very large XML documents more efficiently.

It provides an a couple utilities over SAX and DOM that allow you to only build DOM fragments for the subtrees of the Document you'll actually be transforming against. It currently has a simple facility that reads the XSLT template for matches and keys to build a list of elements, it then passes matched sax events onto a DOMBuilder to build a DOM fragment, this DOM fragment can then can be transformed against using the same XSLT.

-Mark Diggory

Schwartz, Rechell R, ALABS wrote:
Based on the various responses I received I attempted to use SAX to transform an XML file to a SAXResult, and then to read in the SAXResult as
an input source to a second transformation, and transform the document to
HTML to be sent to the browser. I received an error complaining that the ContentHandler of the SAXResult was null. Obviously, I don't' understand how to set/use  a ContentHandler, and I obviously don't understand how to convert a SAXResult to a SAXSource. Here is the relevant code snippet in my JSP page. Any help for a newbie like me would be greatly appreciated, as I am at my wits end. Also, if this is not the right newsgroup for this problem, please direct me to the right place.

String xsl = "/stylesheets/data.xsl";
String xsl2 = "/stylesheets/format.xsl";
Templates templates = (Templates)application.getAttribute("templates");
Templates templates2 = (Templates)application.getAttribute("templates2");
if (templates == null) {
TransformerFactory tfactory = TransformerFactory.newInstance();
templates = tfactory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl)));
application.setAttribute("templates", templates);
}
if (templates2 == null) {
TransformerFactory tfactory = TransformerFactory.newInstance();
templates2 = tfactory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl2)));
application.setAttribute("templates2", templates2);
}
Transformer transformer = templates.newTransformer();
SAXResult saxResult = new SAXResult();
transformer.transform(new StreamSource(new StringReader(hand.detailresult)), saxResult);
transformer = templates2.newTransformer();
transformer.transform(new StreamSource(saxResult.getSystemId()), new StreamResult(out));

-----Original Message-----
From: Rob Rohan [mailto:me@xxxxxxxxxxxx] Sent: Friday, June 20, 2003 3:52 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Optimizing Trax


On Fri, 2003-06-20 at 12:22, Schwartz, Rechell R, ALABS wrote:

How do I tranform the document using SAX-- i.e., how do I change this
line?

transformer.transform(new DOMSource(document), new StreamResult(out));


This is kind of more a java question than an XSLT question. Here is a snip-it that I use in one of my programs - it's a touch kludgey but if you know java you can decipher the needed parts - if you can't I recommend "Java & XML" second edition by Brett McLaughlin from O'Reilly

public long transform(java.io.InputStream xmlin, java.io.InputStream
xslin, java.io.OutputStream output, java.io.File xslURI, java.io.File
xmlURI) throws Exception {
strResult = null;
if(params == null)
params = new Vector();
String media = null;
//set the factory to the specified one System.setProperty("javax.xml.transform.TransformerFactory", XSLTFactory);
//get an instance and make a result object tfactory = javax.xml.transform.TransformerFactory.newInstance();
strResult = new javax.xml.transform.stream.StreamResult(output);
stf = (javax.xml.transform.sax.SAXTransformerFactory) tfactory;
StreamSource ss = new StreamSource(xslin);
if(xslURI != null) ss.setSystemId(xslURI);
stylesheet = tfactory.newTemplates(ss);
transformer = stylesheet.newTransformer();


//set params for(int i=0; i < params.size()-1; i++)
transformer.setParameter((String) params.elementAt(i), (String)params.elementAt(i+1));


//do the transformation StreamSource input = new StreamSource(xmlin);
if(xmlURI != null) input.setSystemId(xmlURI);


transformer.transform(input, strResult);
....




Rechell Schwartz

-----Original Message-----
From: J.Pietschmann [mailto:j3322ptm@xxxxxxxx]
Sent: Friday, June 20, 2003 3:13 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Optimizing Trax


Schwartz, Rechell R, ALABS wrote:


I am using Trax to transform my xml files, and am finding the

performance


very slow especially for large files. Are there any ways of optimizing

the


performance such as by pre-compiling the stylesheets?

Yes, but your time is probably wasted elsewhere.



> transformer = tFactory.newTransformer(new StreamSource(getClass().getResourceAsStream(xsl)));
> transformer.transform(new DOMSource(document), new
StreamResult(out));
^^^^^^^^^^^^^^^^
Use a profiler to check where your program wastes time:
- DB access or whereever you get your raw data
- DOM construction
- transformation
If it is the latter, check your XSL for inefficient constructs.
If it is DOM construction, use SAX. Also DOM uses a lot of memory,
which may be a problem in itself (more GC).


J.Pietschmann


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread