caucho
 XSL API


Applying XSL is a multi-step process:

  1. Create a javax.xml.transform.TransformerFactory
  2. Parse a javax.xml.transform.Templates (Stylesheet)
  3. Create a javax.xml.transform.Transformer
  4. Set any XSL variables
  5. Create a javax.xml.transform.Source for the input.
  6. Create a javax.xml.transform.Result for the output.
  7. Call transform() to transform the input to the output.

The transformers let you select the output format appropriate for your application: output stream, string, or node. In addition, each transformer instance lets you set XSL parameters (i.e. parameters set by xsl:param.)

Java API for XSL transform

import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;

import com.caucho.vfs.*;
import com.caucho.xml.*;
import com.caucho.xsl.*;

public class Test {
  public static void main(String []args)
    throws Exception
  {
    TransformerFactory factory = TransformerFactory.newInstance();

    StreamSource xslSource = new StreamSource("test.xsl");
    // Load the stylesheet.  If it's already compiled, Resin will just
    // load the compiled class.  More sophisticated application will
    // cache the Templates object.
    Templates stylesheet = factory.newTemplates(xslSource);

    // Create a transformer.  This could also be called from a
    // cached Templates object.  The Transformer should not be cached.
    Transformer transformer = stylesheet.newTransformer();

    // Sets a parameter for the xsl:param "source"
    transformer.setParameter("source", "java");

    // The source is a file.
    StreamSource source = new StreamSource("test.xml");

    // The result is a file.
    StreamResult result = new StreamResult("test.html");

    transformer.transform(source, result);
  }
}

Configuring JAXP for Xalan

JAXP is a standard interface which supports pluggable XML and XSL implementations. JAXP selects the parser based on system properties. You can set the properties to select a different parser than the default one.

JAXP Properties for Resin
system propertyResin value
javax.xml.parsers.DocumentBuilderFactory com.caucho.xml.parsers.XmlDocumentBuilderFactory
javax.xml.parsers.SAXParserFactory com.caucho.xml.parsers.XmlSAXParserFactory
javax.xml.transform.TransformerFactory com.caucho.xsl.Xsl

JAXP Properties for Xalan/Xerces
system propertyXerces value
javax.xml.parsers.DocumentBuilderFactory org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
javax.xml.parsers.SAXParserFactory org.apache.xerces.jaxp.SAXParserFactoryImpl
javax.xml.transform.TransformerFactory org.apache.xalan.processor.TransformerFactoryImpl

The resin.conf and web.xml will let you configure system properties on a per-application basis.

Summary

  • Transformers select different output formats.

Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.