Java Reference
In-Depth Information
The noargument newTransformer() method copies source input to the destina-
tionwithoutmakinganychanges.Thiskindoftransformationisknownasthe identity
transformation .
To change input, you need to specify a stylesheet, and you accomplish this task by
callingthefactory's Transformer newTransformer(Source source) meth-
od,wherethe javax.xml.transform.Source interfacedescribesasourceforthe
stylesheet. The following example demonstrates this task:
Transformer t = tf.newTransformer(new StreamSource(new
FileReader("recipe.xsl")));
This example creates a transformer that obtains a stylesheet from a file named re-
cipe.xsl via a javax.xml.transform.stream.StreamSource instance
connectedtoafilereader.Itiscustomarytousethe .xsl or .xslt extensiontoidenti-
fy XSLT stylesheet files.
The newTransformer() methodsthrow TransformerConfigurationEx-
ception whentheycannotreturna Transformer instancethatcorrespondstothe
factory configuration.
After obtaining a Transformer instance, you can call its void setOut-
putProperty(String name, String value) method to influence a trans-
formation. The javax.xml.transform.OutputKeys class declares constants
forfrequentlyusedkeys.Forexample, OutputKeys.METHOD isthekeyforspecify-
ingthemethodforoutputtingtheresulttree(asXML,HTML,plaintext,orsomething
else).
Tip To set multiple properties in a single method call, create a
java.util.Properties object and pass this object as an argument to Trans-
former 's void setOutputProperties(Properties prop) method.
Properties set by setOutputProperty() and setOutputProperties()
override the stylesheet's xsl:output instruction settings.
Before you can perform a transformation, you need to obtain instances of classes
thatimplementthe Source and javax.xml.transform.Result interfaces.You
then pass these instances to Transformer 's void transform(Source xm-
lSource, Result outputTarget) method, which throws an instance of the
javax.xml.transform.TransformerException class when a problem
arises during the transformation.
Thefollowingexampleshowsyouhowtoobtainasourceandaresult,andperform
the transformation:
Search WWH ::




Custom Search