How to transform XML data with XSLT

idea bulb - xslt transformThe example executes an XSL transform on a source XML document which generates a new result. This example has a number of prerequisites:

  • Define an XML document structure – defines your input XML document.
  • Writing an XSLT Transform – defines the rules to transform the input XML document into a output document.
  • Opening/Loading an XML document – This is required to load the source XML document and the XSL Transform document.


The Java example uses the Source and Result interface to designate the inputs and outputs. The actual Source/Result definitions are implementation dependent.

package xmltutorial.xslt;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

public class TransformExample {

public static void transform(String factoryClassname, Source source_xml, Source source_xslt, Result result_out) throws TransformerConfigurationException, TransformerException {
TransformerFactory transformerFactory = null;
if (factoryClassname == null || factoryClassname.isEmpty()) {
transformerFactory = TransformerFactory.newInstance();
} else {
transformerFactory = TransformerFactory.newInstance(factoryClassname, Thread.currentThread().getContextClassLoader());
Transformer transformer = transformerFactory.newTransformer(source_xslt);
transformer.transform(source_xml, result_out);

As a side note: the transformer factory to use is dependent on your implementation. If no transformer factory is specified then the newInstance() method returns the default transformer factory for the JVM/platform.


The C# XSLT example illustrates 3 concepts:

  • Parse and load XML document from a string source into memory. This is done for both the source document and the transform document
  • Apply an XSL transform to the source document – generating a result
  • Write the result to a writer (or if required a stream). This happens at the same time as the transform.
/// <summary>
/// Transforms an XML string document to a target document using XSLT.
/// </summary>
/// <remarks>
/// Writes the result to a file on the local file system
/// </remarks>
/// <param name="filepath">The target file path</param>
public void writeXML(String filepath) {
string str_xmlsource = this.getSourceDocument();//XML document string
string str_transform = this.getTransformDocument(); // XSLT document string

using (StringReader reader_xml = new StringReader(str_xmlsource)) {
XPathDocument xml_obj = new XPathDocument(reader_xml);
using (StringReader reader_xslt = new StringReader(str_transform)) {
XPathDocument xml_xslt = new XPathDocument(reader_xslt);
XslCompiledTransform xml_transform = new XslCompiledTransform();
using (XmlTextWriter xml_writer = new XmlTextWriter(filepath, null)) {
xml_transform.Transform(xml_obj, null, xml_writer);

Visual Basic

No example – YET…

This is an ongoing project – Be sure to follow our site as we are constantly adding new content just for you! If you have a special request, contact us and let us know!

Posted in XSLT and tagged , , .

Leave a Reply