How to use XSLT to transform XML documents using Groovy

Document created by gbockelmann Employee on Aug 2, 2011Last modified by Adam Arrowsmith on Mar 2, 2016
Version 3Show Document
  • View in full screen mode
The following Groovy script can be used mid-process in a Data Process >> Custom Scripting step to convert source XML Document data based on an external XSLT style sheet residing on the local Atom machine and output the resulting transformation.

 

------

 

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.

 

**THIS SCRIPT MAY ONLY BE USED BY PROCESSED DEPLOYED TO LOCAL ATOMS.
IT CANNOT BE USED BY PROCESSES DEPLOYED TO THE ATOM CLOUD.**

 

If there are problems loading/compiling the style sheet, 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.

 

**SPECIAL NOTE FOR USE WITH XSL STYLESHEET VERSION 2.0**
If your stylesheet uses XSL 2.0 or higher, you will need to download and unzip
a special Saxon library from the Boomi AtomSphere Knowledge Base and copy
it into the Atom's ../<Atom root>/userlib/script directory (you will need to create
the directory if it doesn't exist already).

/*
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.

**THIS SCRIPT MAY ONLY BE USED BY PROCESSED DEPLOYED TO LOCAL ATOMS.
IT CANNOT BE USED BY PROCESSES DEPLOYED TO THE ATOM CLOUD.**

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.

**SPECIAL NOTE FOR USE WITH XSL VERSION 2.0**
If your style sheet uses XSL 2.0 or higher, you will need to download
updated Saxon libraries and copy them in the Atom's lib directory.
1. Download Saxon libraries from here: http://saxon.sourceforge.net/
(Saxon-B v9.1.0.8 is fine).
2. Extract the files and copy them to your local Atom's ../<Atom root>/userlib/script
directory.
3. Restart the Atom.

*/

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);
}

3 people found this helpful

Attachments

Outcomes