For example, this code fragment uses the XSLT stylesheet found at
http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl
to transform the file people.xml in the current
working directory onto System.out:
TransformerFactory factory = TransformerFactory.newInstance( );
URL u = new URL(
"http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl");
InputStream in = u.openStream( );
Source stylesheet = new StreamSource(in);
Transformer xform = factory.newTransformer(stylesheet);
InputStream people = new FileInputStream("people.xml");
Source original = new StreamSource(people);
Result transformed = new StreamResult(System.out);
xform.transform(original, transformed);
The procedure is much the same when the source or result is a DOM
Document object or a SAX event stream. Just use
the DOMSource, SAXSource,
DOMResult, and/or SAXResult
classes as appropriate. For example, this code fragment transforms
the DOM Document object doc
according to the stylesheet at http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl
and passes the result through the SAX
ContentHandler object named
handler:
Document doc;
// Build the doc object in the usual way...
TransformerFactory factory = TransformerFactory.newInstance( );
URL u = new URL(
"http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl");
InputStream in = u.openStream( );
Source stylesheet = new StreamSource(in);
Transformer xform = factory.newTransformer(stylesheet);
ContentHandler handler = new XMLCounter( ); // From Chapter 19
Source original = new DOMSource(doc);
Result transformed = new SAXResult(handler);
xform.transform(original, transformed);
 |  |  |
23.3. XSLT Functions |  | 24. DOM Reference |