Transforming XML with XSLT


StreamSource input =    new StreamSource(new File("document.xml")); StreamSource stylesheet =    new StreamSource(new File("style.xsl")); StreamResult output =    new StreamResult(new File("out.xml")); TransformerFactory tf =    TransformerFactory.newInstance(); Transformer tx = tf.newTransformer(stylesheet); tx.transform(input, output);



XSLT is a standard for transforming XML documents from one format to another using an XSL stylesheet. The javax.xml.transform package is the API for using the XSLT transformation standard in Java. XSL stands for Extensible Stylesheet Language. XSLT is XSL Transformation, and it allows you to completely restructure an XML document. In general, when using XSLT, you have an input XML document and an input XSL stylesheet; together, these produce an output XML document. However, the output document type is not limited to XML. You can produce many types of output documents using an XSL transformation.

In this phrase, we create StreamSource instances for the documents, which are input into the transformation process. These are the XML document to be transformed and the XSL stylesheet that contains the transformation instructions. We also create a StreamResult object, which will be used to write the output document to. We then obtain an instance of a TRansformer object, generated from a TRanformerFactory instance. We pass the stylesheet stream into the newTRanformer() method of the TRansformerFactory object to create our transformer object. Finally, we call the transform() method of the transformer to transform our input XML document into the output document styled with the stylesheet we selected.

We don't go into details of what an XSL stylesheet looks like or how to create one. An excellent reference for learning more about XSL stylesheets and XSLT is Java and XSLT by Eric Burke.

XSL can be a powerful technology for developers. For example, suppose you have a web application that must be accessed from a variety of devices, including a PDA, a web browser on a PC, and a cell phone. Using XSLT, you could transform your output to a format suitable for each of these devices without having to specifically code output for each device type separately. XSLT is also very useful in creating multilingual sites. You can transform XML output into a variety of languages using XSLT transformations.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net