How to convert a Multipart into an XML using XSLT?

Posted by nithinonpsdn on 03-Jun-2010 05:06

I have a process which results in a Multipart message but without content-id.

Please help in transforming such a Multipart without CONTENT-ID to an XML.

All Replies

Posted by Bill Wood on 04-Jun-2010 16:57

The XML Transformation Service has the option for choosing what part to apply the stylesheet to.  You can use the part's content-id, or you can enter the Part index.

See Workbench help on

Configuring and deploying a transformation service

Parameter NameDescriptioin
Default Message Part

Optionally, to specify the message part to                         transform, enter either:

  • The index of the message part for  the                            service to transform. The default                            value is message part index                            0,  which directs the                            transformation service to transform                            the first message part (index =                            0).

  • All or                            -1 to transform all                            parts of the message.

  • The content ID  of the message part                            you want to transform.

Alternatively, you can specify the message                      part in the JavaScript rule file.

Posted by nithinonpsdn on 06-Jun-2010 09:40

Dear Wood,

        I wanna convert the whole multipart message into an XML.

For instance.,

Let us consider the following Multipart Message :

Part 0 :

      NITHIN

Part 1:

       UK

I want to transform this multipart into the following message :

       NITHIN

       UK

Pls help me in this regard. Kindly let me know if I am not clear.

Posted by Bill Wood on 06-Jun-2010 10:08

I see your issue now. 

First, I will say it is easier to convert these if they are in one part.  I don't know if this is relevant to you, but if some of the parts are created by a call out or web service invoction, it is better to use the "Insert as Child Element" option on the call, rather than "Add New Part".   They you have just one XML and can convert it.


But sometimes that is not possible and you have two parts that you want to use as two inputs to an XML Transformatino (XSLT) Stylesheet.   The way to do this in XSLT is to define one part as the xslt input, but have the others be 's.   So you need to have a styleshet that uses .  In workbench, define the XSLT as having multiple inputs or add the xsl:param statement yourself.  You can test it with dummy data (where each maps to a variable in the scenario).

So the next part is how do you pass parameters into the stylesheet when the parameters are variable based on input.  (Static parameters can be passed in the "Default Stylesheet Parameters" configuration as name=value;name=value).   For dynamic ones, you need to use the JavaScript Rules File.  (Check the Workbench doc on Progress Sonic Workbench User Guide > Progress Sonic ESB Services > XML transformation service > Configuring and deploying a transformation service for more info).

Basically, you need a rules file like the following -- I'm away from my computer with Sonic, so I'm doing the code sort of from memory.

function rule()
{

  myArray = new java.util.ArrayList();

  //Create a Params HashMap and add name value pairs in it.

  paramsHashMap = new java.util.HashMap();
  paramsHashMap.put("param1",XQMessage.getPart(1).getContent());  // Param1 is your part1

  //Create MsgPartIndex Integer
  msgPart = new java.lang.Integer(0);  // Main is Part 0
  myArray.add("sonicfs:///yourStyleSheet.xsl");
  myArray.add(msgPart);
  myArray.add(paramsHashMap);
  return myArray;

}

Posted by mjabali on 06-Jun-2010 10:08

Hi Nithin,

Sometime ago I wrote a tutorial explaining a similar use case like you described. Here is a snapshot that I believe you can use to solve your problem...

When processing a multipart message, the Sonic ESB XML Transformation Service typically takes a single message part and applies an XSLT transformation to it. Alternatively the same transformation can be
applied to each message part individually.

However, occasionally it is necessary to collate information from several message parts into a single result. The following solution describes a means of achieving this using the built-in XML Transformation Service.

The solution described here passes one message part as the source document on which the XSLT acts and two additional message parts as stylesheet parameters, thereby allowing a single XSL transform access
to all three message parts.

The XSL parameter values are passed to the transformation engine using a JavaScript Rule File.

The example here references the first three parts in a hypothetical multi-part message: part 0, part 1, and part 2.

The JavaScript Rule File sets up the parameters for the transformation. It consists of a rule() function which gets called by the Transformation Service:

function
rule() {

    // Get parts 1 and 2 from the incoming message -
    // part 0 is referenced later.
    // The xpath expressions can be more selective
if

    // the stylesheet doesn't need access to the whole
    // of each message part
    part1Doc = XQ_getXPath("/", 1);
    part2Doc = XQ_getXPath("/", 2);
   
    // Set up a map containing the parameters to pass
    // to the stylesheet.  The parameter names match
    // those declared
in
the stylesheet.  The values
    // consist of the message parts retrieved above.
    paramsMap =
new
java.util.HashMap();
    paramsMap.put("MsgPart1", part1Doc);
    paramsMap.put("MsgPart2", part2Doc);

    // Build the complete array to be returned by the
    // rule
function
.  This consists of:
    //   - the path to the XSL file
    //   - the number of the message part to which the
             XSL will be applied -
in this case
part 0
    //   - the stylesheet parameters map created above
    resultArray =
new
java.util.ArrayList();
    resultArray.add("sonicfs:///Resources/Merge.xsl");  // stylesheet
    resultArray.add(
new java.lang.Integer
(0));       // message part
    resultArray.add(paramsMap);                      // parameters

   
return
resultArray;

  }

The above code should be saved as a .js file in the SonicFS storage and referenced by the Transformation Service's 'JavaScript Rule File' parameter.

If the Transformation Service is called from an ESB Process then the JavaScript Rule File can be specified as a runtime parameter for the step in the process definition.

The XSL (referenced in the above rule file, e.g. Merge.xsl) now has access to all three message parts as follows:

  <
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version=
"1.1"
>
   
 
 

 


 

 

   

   
     

     


     

     

     

     

      ...
     
   

 

Hope this helps,

-Marcelo

Posted by nithinonpsdn on 07-Jun-2010 08:11

Thanks a lot William!!!

Posted by nithinonpsdn on 07-Jun-2010 08:12

Thanks a lot Marcelo!!!

This thread is closed