|
| | | | Why do XSLTProcessor and StylesheetRoot both have process() methods to perform transformations? | | | | |
| | To perform a transformation, Xalan-Java uses XML input, an XSL stylesheet, and XSLTProcessor. In one scenario, you use
XSLTInputSource class objects as containers for the XML input and the XSL stylesheet. To perform the
transformation, the XSLTProcessor interface object "compiles" the stylesheet, producing a StylesheetRoot class object, but you the user have no direct interaction with the StylesheetRoot.
Example:
| | | | import org.apache.xalan.xslt.*;
...
XSLTProcessor proc = XSLTProcessorFactory.getProcessor();
proc.process(new XSLTInputSource("foo.xml"),
new XSLTInputSource("foo.xsl"),
new XSLTResultTarget("foo.out"); | | | | |
Second scenario: you want to perform a number of transformations with the same stylesheet. To improve performance, you
compile the stylesheet once, and use the resulting StylesheetRoot class object for each
transformation.
You can use the
XSLTProcessor process() method with a compiled stylesheet. For an example, see Compiling stylesheets. But an XSLTProcessor object is not thread-safe and must be
reset between transformations. A StylesheetRoot object, on the other hand, is thread-safe and may even be used concurrently
(running in a servlet, for example) to perform multiple transformations, so it is more straightforward to use the
StylesheetRoot process() method to perform the transformations.
Example:
| | | | import org.apache.xalan.xslt.*;
...
// Pass in an array of XML input file names and an
// "uncompiled" stylesheet file name.
public multiTransform(String[] xmlInputFiles, String xslFile)
{
XSLTProcessor proc = XSLTProcessor.Factory.getProcessor();
StylesheetRoot style = proc.ProcessStylesheet
(new XSLTInputSource(xslFile));
for (int i = 0; i < xmlInputFiles.length; i++)
{
style.process(new XSLTInputSource(xmlInputFiles[i],
new XSLTResultTarget(xmlInputFiles[i] + ".out"));
}
}
... | | | | |
|
|