Is it possible to use XML transformation (with MessageExtens

Posted by jnyholt on 04-Mar-2011 10:14

I have developed a custom service but I want to be able to apply an XML transformation with the ability to use all the HeaderExtensions and MessageExtensions. (I have it working with the standard javax.xml.transform libs but this doesn't give me access to the header and message extensions which I require. It is required to complete the transformation inline with the service type to reduce the size of a potentially large message -- because of this applying the stylesheet in a second step is not an option.)

Any suggestions or sample code would be appriciated.

All Replies

Posted by tvanhane on 28-Mar-2011 03:18

The standard Sonic ESB message and header extensions can be used in "own" transformations as well, as long as Saxon libraries are on the classpath (Saxon is included with Sonic ESB).

The trick is to set the "XQMessage" parameter to the transformation context, so that Sonic's extensions are able to find the XQMessage.

This is how I've done the transformation in a custom service:

TransformerFactory fac = TransformerFactory.newInstance();
try {
     //stylesheetContent is a String containing the stylesheet content (from a custom service parameter)
     Transformer tr = fac.newTransformer(new StreamSource(new StringReader(stylesheetContent)));
     StringWriter sw = new StringWriter();
     StreamResult streamResult = new StreamResult(sw);

     tr.setOutputProperty(OutputKeys.INDENT, "yes");
     
     //xqMessage is the current XQMessage
     tr.setParameter("XQMessage", xqMessage);
     tr.transform(new StreamSource(new StringReader(result)), streamResult);
     
     //Put the result XML in a String
     res = sw.toString();
}
catch (Exception ex) {
     throw new RuntimeException(ex);
}

- Tatu V.


This thread is closed