XSLT provides a way to transform a XML document into various format types. In this screencast find out how to convert XML into HTML using a java based XSLT transformer.
Detailed Video Notes
XSL stands for Extensible Stylesheet Language and can be used to transform XML documents into different types, similar to how CSS works with HTML. In other words if you have an XML input and XSL file you can produce new documents in different formats. In this tutorial we will walk through transforming XML into HTML using Apache's XALAN implementation.
Java by nature provides a spec or an interface where vendors and open source projects create implementations to consume. Transforming xml w/ XSLT is no exception. Popular java processor implementation, some with a cost while others are open source, include SAXON, apache cocoon, Context framework and apache XALAN which we will be using in our examples.
Once we have xml we need the view or the template which the xml will bind to so let's create a books.xsl. This tutorial will focus on converting to html but know you could substitute various output types such as PDF, XML, Microsoft word document or just about any output type. We won't dive into detail to explain what each XSLT element it as it can be found on mozilla xslt elements reference site. We did include a reference to boostrap for table styling
Next let's write the java code that will take the XML and XSL style sheet to produce an HTML document. The TransformerFactory is the main class to do the transformation and was created in a way to allow consumers to configure with various providers. Each implementation may differ on which configuration options are allowed so be aware if you plan on supporting multiple implementation libraries.
StreamSource is an abstraction class so the same parsing code and be used from various sources like DOMSource, SAXSource, StAXSource or JAXBSource. We will read in the books.xml and books.xsl files using java 7 syntax passing them into into a StreamSource. The final piece before making the transformation is where should the output go. Initializing a StreamResult by converting a Paths to a file we will output the contents of the transformation to myfile.html. The final step is obtaining a transformer and passing in the data, the template and the output. Let's run this code and view the output in chrome.
Outputting as ByteArray
If you are working within a web context you may want to write your results to the HttpServletResponse as a byte array. Below is a code snippet that could help get you started.
Hope you enjoyed today's level up, have a great day!