How to use XSLT to transform XML documents using Groovy

Document created by gbockelmann Employee on Aug 2, 2011Last modified by Adam Arrowsmith on Nov 24, 2017
Version 4Show Document
  • View in full screen mode

The following Groovy script can be used in a Data Process shape Custom Scripting step to convert a source XML Document data based on an external XSLT stylesheet file residing in a local disk directory accessible by the Atom.

 

Note: As of the April 2017 release which introduced Custom Library components, you can use this script in the Dell Boomi Atom Cloud in addition to local Atoms. However the XSLT stylesheet should be placed in the cloud Atom's ../work directory. To place this file, create a simple process to write the XSLT file using a Disk connector.

 

 

Prerequisites

The scripts require additional third-party Java libraries to be deployed to the Atom.

  1. If your stylesheet uses XSL 2.0 or higher, you will need to download updated Saxon libraries from here: http://saxon.sourceforge.net/. Be sure to get the Java version, "source" is not needed.
    • For example: SaxonHE9-8-0-6J.zip
  2. Upload the following jars to the Setup > Account Libraries:
    • saxon9he.jar
  3. On the Build tab, create a new Custom Library component, configured as Type="Scripting", and include the jar files added to the Account Libraries above:
  4. On the Deploy tab, attach and deploy the Custom Library component to the desired Environment/Atom:

 

Script

/*
This script converts source XML Document data based on an external
XSLT style sheet residing on the local Atom machine and outputs the
resulting transformation.

If there are problems loading/compiling the style sheet or parsing the
input Document data, a fatal exception will be thrown. If there are problems
transforming an individual document, the TransformationException message
will be captured in a User Defined Document Property named "XSLT_ERROR"
which can be inspected in a subsequent Decision step to determine
Document-level success. Also, if an individual Document transformation
fails, the original input Document data will be returned.
*/


import org.jdom.transform.XSLTransformer;
import org.jdom.transform.XSLTransformException;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

// Initialize external XSLT document - CHANGE TO POINT TO YOUR STYLE SHEET
String xsltFileName = "C:\\Test\\My XSLT Style Sheet.xslt";
XSLTransformer transformer = new XSLTransformer(xsltFileName);

// Loop through each document
for ( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  Properties props = dataContext.getProperties(i);

  // Build XML Document
  SAXBuilder builder = new SAXBuilder();
  Document inputDoc = builder.build(is);

  Document resultDoc;

  try {

    resultDoc = transformer.transform(inputDoc);

  } catch (XSLTransformException e) {

    // If exception thrown during transformation, store the exception's message in a
    // User Defined Document Property "XSLT_ERROR" to inspect in the next Process step
    // and return the original document data.
    props.setProperty("document.dynamic.userdefined.XSLT_ERROR", e.getMessage());
    resultDoc = inputDoc;
  }

  // Output XML data
  XMLOutputter outputter = new XMLOutputter();
  is = new ByteArrayInputStream(outputter.outputString(resultDoc).getBytes("UTF-8"));

  dataContext.storeStream(is, props);
}
7 people found this helpful

Attachments

Outcomes