Monday, October 5, 2015

Parse and Read the contents of Email Attachment in the BPEL process

ParseAndReadAttachmentContent

This post assumes that you already have some working knowledge of BPEL and XSLT functions and you're able to retrieve the content of Email attachment as described in my previous post - UMS Adapter - All about email attachments.

Many a times the requirement is to parse through the content of the attachment to extract desired information out of it. As attachment read is in the base64 binary data by default, first and foremost thing is to convert it into String and then parse it into the required XSD schema. Oracle provides a function Out-of-the-box (OOTB )to do both the bits in one call. Follow below steps to parse the incoming attachment.

The simple case I have taken here is that I receive a CSV file as attachment in email and I parse it into XML for further transformations on the content.

  • Create a schema of the email attachment that you want to parse using Native Schema builder. Basically that means dragging and dropping a File Adapter and create a schema with that, later cancel or delete the adapter as we only need the schema so crated. I receive a comma delimited file as attachment, hence used a delimited option to create my schema but you can also parse XML, Fixed Length any other type of file. In case of XML files, please pay attention to the XML file namespace and the target Schema namespace .
  • Create a variable targetMessage of the schema created above (shown below in Examples)
  • Use the OOTB function ora:doTranslateFromNative(Input Message in binary format, Relative path to the schema from composite directory, Schema Root Element Name, 'DOM' ) as shown below
  • The first assign is to fetch the Attachment Content into another Custom variable of the same type
  • $targetMessage - is the Element Type variable of the target schema (Root element TargetRequest) which will hold the translated message
  • Once the targetMessage is formed, you can traverse through all elements of this message and carry out all desired transformation operations on this like any other XML variable.

Below shown is the structure of file and the associated code, although not full but should give an idea of the main components involved.

Example Email Attachment format :

Example Schema :

Variable Created in BPEL:

Please let me know if any issues and I'll try to help! Happy Emails!

3 comments:

  1. Hi,

    thanks for the useful post :)
    when you say "In case of XML files, please pay attention to the XML file namespace and the target Schema namespace ." what does that mean ? the schema which which we create from CSV file should match the target namespace of BPEL process ?

    ReplyDelete
    Replies
    1. I am getting this error while using the function


      XPath expression failed to execute. An error occurs while processing the XPath expression; the expression is ora:doTranslateFromNative($inputVariable.payload/client:input,'xsd/test_3.xsd', 'RootElement', 'DOM'). The XPath expression failed to execute; the reason was: Translation Failure. [Line=1, Col=13] Translation from native failed. . The incoming data does not conform to the NXSD schema. Please correct the problem. {Usage=[ora:doTranslateFromNative('input string variable or attachment element', 'nxsd template', 'nxsd root element name', 'DOM or SDOM or ATTACHMENT', 'optional attachment element if the targetType paramater is ATTACHMENT?')]}. Check the detailed root cause described in the exception message text and verify that the XPath query is correct.

      Delete
  2. Hi,

    thanks for the useful post :)
    when you say "In case of XML files, please pay attention to the XML file namespace and the target Schema namespace ." what does that mean ? the schema which which we create from CSV file should match the target namespace of BPEL process ?

    ReplyDelete