Java API for XML (JAXP) – XSLT Transformation

XSLT Introduction

XSLT is the language for transforming XML documents into other documents such as HTML or other XML documents. It is part of the XSL or XML Schema language. The input to the transformation is a source tree and the output is the result tree. It uses predefined templates to match parts of the source document. The matched parts are then converted to the result document


JAXP provides classes that define the transformation API. i.e. It specifies a transformer factory that can be used to obtain a transformer. The transfomer takes in an XSL. The transformer has a transform method that converts the source tree to result tree. JAXP provides classes for the Source tree and the result tree


In this tutorial we look at using the JAXP classes that convert the XML to an HTML using an XSL. we show three examples that use a DOMSource, SAXSource and StAXSource respectively. The result tree will be printed on the console. The bbc.xsl file can be found here

package com.studytrails.xml.jaxp;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stax.StAXSource;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class JaxpTransformationExample4 {
	private static String xmlSource = "";

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException, XMLStreamException {
		JaxpTransformationExample4 example = new JaxpTransformationExample4();
		// transforming the XML into HTML using a DOM Source
		// transforming the XML into HTML using a SAX Source
		// transforming the XML into HTML using a StAX Source


	public void transformDom() throws ParserConfigurationException, SAXException, IOException, TransformerException {
		// create the DOM Source
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document bbcDoc = builder.parse(xmlSource);
		DOMSource source = new DOMSource(bbcDoc);

		// Create an instance of the TransformerFactory
		TransformerFactory transfomerFactory = TransformerFactory.newInstance();
		// prints
		// obtain the XSLT transformer. The transformer has instructions for
		// converting the XML to HTML
		Transformer transformer = transfomerFactory.newTransformer(new StreamSource("bbc.xsl"));
		// An object to hold the results. It can be a file. In This example we
		// output to console.
		StreamResult result = new StreamResult(System.out);
		transformer.transform(source, result);

	public void transfromSAX() throws ParserConfigurationException, SAXException, MalformedURLException, IOException, TransformerException {
		// Create a SAXSource from the xml
		SAXSource saxSource = new SAXSource(new InputSource(new URL(xmlSource).openConnection().getInputStream()));
		// Object to hold the result
		StreamResult result = new StreamResult(System.out);
		// the factory that provides the transformer
		TransformerFactory factory = SAXTransformerFactory.newInstance();
		// the transformer that does the transformation
		Transformer transformer = factory.newTransformer(new StreamSource("bbc.xsl"));
		// the actual transformation
		transformer.transform(saxSource, result);


	public void transformStAX() throws XMLStreamException, IOException, TransformerException {
		// obtain the StAX Source
		XMLInputFactory factory = XMLInputFactory.newFactory();
		URL url = new URL(xmlSource);
		XMLEventReader reader = factory.createXMLEventReader(url.openStream());
		StAXSource staxSource = new StAXSource(reader);

		// The factory that produces the transformer
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer(new StreamSource("bbc.xsl"));
		// An object to hold the results.
		StreamResult result = new StreamResult(System.out);
		// the transformation
		transformer.transform(staxSource, result);

Leave a Comment