Manage this page
Java Links
XSLT links
Satya - Thursday, August 25, 2005 5:54:04 PM
Getting a transform factory
TransformerFactory tf = TransformerFactory.newInstance();
This is the entry point for getting an xslt "Transformer" object to do a transform.
Satya - Thursday, August 25, 2005 5:59:29 PM
A taste of getting a transformer object
Transformer t 
  = tf.newTransformer(
       new StreamSource(
             new StringReader(xsltString)));
Satya - Thursday, August 25, 2005 6:01:01 PM
Three kinds of sources
DOMSource SAXSource StreamSource
Satya - Thursday, August 25, 2005 6:03:03 PM
Possible streamsources
File InputStream Reader a string representing a URL
Notice a string is not a stream source directly. If you do pass a string it will be thought of as a url and a url look up will be done
Satya - Thursday, August 25, 2005 6:04:45 PM
Getting a stream source out of a string
new StreamSource(new StringReader(yourstring));
Notice StringBufferInputStream is deprecated.
Satya - Friday, August 26, 2005 10:04:40 AM
Putting it all together: Transforming an xml string
   public static void transform(String xmlString, 
                     String xsltString, 
                     Writer outputWriter)
   throws TransformException
   {
      try
     {
         TransformerFactory tf = TransformerFactory.newInstance();
       
         Transformer t = tf.newTransformer(
             new StreamSource(
               new StringReader(xsltString)));
         
         t.transform(new StreamSource( 
                   new StringReader(xmlString)),
                     new StreamResult(outputWriter));
     }
      catch(TransformerConfigurationException x)
     {
         throw new TransformException("Error:xslt not properly configured.",x);
     }
      catch(TransformerException x)
     {
         throw new TransformException("Error:xslt transformation error.",x);
     }
   }
