mikedev February 2016

Mule - Transform XML to SOAP message

I receive a JSON message to my mule flow like this:

{
  "book": {
    "author": "Gambardella, Matthew",
    "title": "XML Developer's Guide",
    "genre": "Computer",
    "price": "44.95",
    "publish_date": "2000-10-01",
    "description": "An in-depth look XML"
  }
}

And then I use a JSON to XML transformer with mule and it returns:

   <book >
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look XML</description>
   </book>

This is my actual mule flow:

HTTP --> JSON To XML --> Logger --> WS Consumer

I want to transform that XML to SOAP message adding an URI and a Prefix.

Why I need a Prefix and a SOAP message? I need it to send to the web service and send as follows:

...
    <pref:author>Gambardella, Matthew</pref:author>
...

I tried to add an XSLT component but it return me an error when I use : or &#x3a; (Hex code).

I thought to use the Dataweave (Mapper) component but it only works with Mule Enterprise Edition.

This is the result that I want:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"  xmlns:pref="URI_SOAP_WS">
<soap:Body>
   <pref:book>
          <pref:author>Gambardella, Matthew</pref:author>
          <pref:title>XML Developer's Guide</pref:title>
          <pref:genre>Computer</pref:genre>
          <pref:price>44.95</pref:price>
          &        

Answers


michael.hor257k February 2016

The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pref="URI_SOAP_WS">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" xmlns:pref="URI_SOAP_WS">
        <soap:Body>
            <xsl:apply-templates/>
        </soap:Body>
    </soap:Envelope>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="pref:{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

when applied to your XML example, will return:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" xmlns:pref="URI_SOAP_WS">
   <soap:Body>
      <pref:book>
         <pref:author>Gambardella, Matthew</pref:author>
         <pref:title>XML Developer's Guide</pref:title>
         <pref:genre>Computer</pref:genre>
         <pref:price>44.95</pref:price>
         <pref:publish_date>2000-10-01</pref:publish_date>
         <pref:description>An in-depth look XML</pref:description>
      </pref:book>
   </soap:Body>
</soap:Envelope>

Post Status

Asked in February 2016
Viewed 1,037 times
Voted 7
Answered 1 times

Search




Leave an answer