Search code examples
mulemule-eldataweave

How to embed CDATA in Mulesoft


I'm trying to embed a literal CDATA value in a Mulesoft flow and cannot figure out how to do so.

My desired output (in an HTTP request body) is:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <query xmlns="http://autotask.net/ATWS/v1_5/">
            <sXML>
                <![CDATA[<queryxml><entity>ticket</entity><query><condition><field>id<expression op="equals">12345</expression></field></condition></query></queryxml>]]>
            </sXML>
        </query>
    </soap:Body>
</soap:Envelope>

My Dataweave transformation looks as follows:

%dw 1.0
%output application/xml
%namespace soap http://schemas.xmlsoap.org/soap/envelope
---
{
    soap#Envelope @(version: "1.0") : {                  
        soap#Header: {},
        soap#Body: {
            query: {                
                sXML: "<queryxml><entity>ticket</entity><query><condition><field>id<expression op=\"equals\">12345</expression></field></condition></query></queryxml>"                     
            }
        }
    }
}

But when I send this request to requestb.in (to inspect the contents), I can see it's coming through like this (focus on the sXML entity):

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" version="1.0">
  <soap:Header/>
  <soap:Body>
    <query>     
      <sXML>&lt;queryxml>&lt;entity>ticket&lt;/entity>&lt;query>&lt;condition>&lt;field>id&lt;expression op="equals">12345&lt;/expression>&lt;/field>&lt;/condition>&lt;/query>&lt;/queryxml></sXML>
    </query>
  </soap:Body>
</soap:Envelope>

How can I get a literal CDATA value in there via dataweave / MEL?

Thank you.


Solution

  • I would try:

    sXML: "<queryxml> .... </queryxml>" as :cdata
    

    See https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-formats#custom-types-2 for more information.