Java XML – JDOM2 – XSLTransformation

JDOM2 has classes that perform XSL transformation of the JDOM2 document. The input to the transformation is the JDOM2 document and an XML stylesheet and the output is whatever transformation the stylesheet specifies. In the example below we look at JDOM2 to do HTML transformation. By default, JDOM2 uses the JAXP TrAX classes for transformation. The important classes are :

  • JDOMSource – This class holds the input for the transformation. It can be a Document, node or list of nodes.
  • JDOMResult – This class holds the result of the transformation. It is generally a list of nodes or a JDOM2 document. The result tree may not be a well formed XML document.
  • XSLTranfomer – The class that wraps the transformation. It uses the JAXP TrAX classes for actual transformation. The javax.xml.transform.TransformerFactory java system property determines which XSLT engine is to be used. As specified in the code documentation this could be
    • Saxon 6.x: com.icl.saxon.TransformerFactoryImpl
    • Saxon 7.x: net.sf.saxon.TransformerFactoryImpl
    • Xalan: org.apache.xalan.processor.TransformerFactoryImpl
    • jd.xslt: jd.xml.xslt.trax.TransformerFactoryImpl
    • Oracle: oracle.xml.jaxp.JXSAXTransformerFactory

Lets look at an example of a transformation.The source XML can be downloaded from
here
and the xsl can be downloaded from
here
.

package com.studytrails.xml.jdom;

import java.io.IOException;

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.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamSource;

import org.jdom2.Document;
import org.jdom2.input.DOMBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.transform.JDOMResult;
import org.jdom2.transform.JDOMSource;
import org.xml.sax.SAXException;

public class JdomTransformationExample {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError,
			TransformerException {
		//  read the XML to a JDOM2 document
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder dombuilder = factory.newDocumentBuilder();

		org.w3c.dom.Document w3cDocument = dombuilder.parse("bbc.xml");
		DOMBuilder jdomBuilder = new DOMBuilder();
		Document jdomDocument = jdomBuilder.build(w3cDocument);

		// create the JDOMSource from JDOM2 document
		JDOMSource source = new JDOMSource(jdomDocument);
		
		// create the transformer
		Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("bbc.xsl"));
		
		// create the JDOMResult object
		JDOMResult out = new JDOMResult();
		
		// perform the transformation
		transformer.transform(source, out);

		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
		System.out.println(outputter.outputString(out.getDocument()));
	}
}


Leave a Comment