Welcome to Aspire Knowledge Central (AKC)
Home Public Library Author Content
Java XSLT notes
Click here to provide feedback or comments at the bottom of this page
Getting a transform factory
TransformerFactory tf = TransformerFactory.newInstance();
This is the entry point for getting an xslt "Transformer" object to do a transform.
A taste of getting a transformer object
Transformer t
= tf.newTransformer(
new StreamSource(
new StringReader(xsltString)));
Three kinds of sources
DOMSource
SAXSource
StreamSource
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
Getting a stream source out of a string
new StreamSource(new StringReader(yourstring));
Notice StringBufferInputStream is deprecated.
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);
}
}