Monday, April 6, 2015

XSLT - transforming to text output and writing to File Adapter

Problem:

I have an XSLT which is supposed to transform an XML (polled by a File adapter) to a plain text file. I tried the transformation in several online XSLT test tools (such as xslttest.appspot.com) and the output is shown as expected, so I was inclined to believe that there was nothing wrong with how I was doing the transformation. I used the mediator to do the transformation and passed it to a File Adapter to write the result in a .txt file.

I had another XSLT which transforms to an HTML file via a Mediator and that one worked as it should, but when I would replace the transformation file with an output method of text, the Mediator wouldn't do the transformation and so the File Adapter would give me the following error:

"JCA Binding execute of Reference operation 'Write' failed due to: Input Source null. Input Source null. Input Source is null. Please make sure that the outbound interaction receives a valid payload. ".

I wasn't sure why the XSLT wasn't working for a text output while the HTML output seemed to work fine with the Mediator.

Solution:

What I eventually had to do was pass the payload to BPEL and do the transformation using the ora:processXSLT function in an Assign activity:

ora:processXSLT('xsl/850-to-POI.xsl',$ReceiveFile_Read_InputVariable.body)

Note: The assign activity HAS to be done using this function and not any other variation (e.g. doXSLTransformForDoc etc.) since otherwise the text output from the XSLT will NOT be properly handled! Also in 12c, with the new folder structure, it is NOT necessary to add the two periods to access the Transformations folder (e.g. ../Transformations/TransformationFile.xsl). Doing so will cause for the XSLT file to not be recognized.

I copied this result to a String variable called "decodedBody", then I used Java to encode the contents to a base64binary variable called "encodedBody" (I tweaked a bit the very useful encoding function found in this blog):

addAuditTrailEntry("Base64encode started");       
    
try     
{       
String input = (String) getVariableData("decodedBody");       
addAuditTrailEntry("Base64decode decoded="+input); 

oracle.soa.common.util.Base64Encoder encoder = new oracle.soa.common.util.Base64Encoder();   
String encoded = encoder.encode(input);       
      
setVariableData("encodedBody",encoded);    
}    
     
catch (Exception e)     
{       
  addAuditTrailEntry("Base64encode Exception: "+e.getMessage());       
}       
    
addAuditTrailEntry("Base64encode ended");

I would then pass the encodedBody to the File Adapter input variable (which had an opaque schema). This would write the positional flat file I needed to a plain .txt. :)

I'm not sure if there is a better way to do this by just using the Mediator, so if anyone has any tips please do let me know in the comments!

No comments:

Post a Comment