Search code examples
muledataweavemule4

How to remove special characters from an XML output?


I'm facing an issue of special characters being added to output payload whenever i'm doing transformation to xml. see below sample for your reference.

I want to populate the exact value whit no special characters being added. As well, is there a function to be applied to trim and remove special characters from payload in case of any?

Input Payload

{
  "Order" : {
    "Id" : 215,

    "Line" : [ {
     "Desc" : "d&Bf 5489-lk45 brand apple 13 pieces",
      "buyerDesc" : ""
    }
    ]
    }
    }

Used Dataweave Script

%dw 2.0
output application/xml escapeGT= true
---
{
    "Order": {
        id: {
    Detail:  {
    Line: payload.Order.Line map {
      
    frame:{
Desc: $.Desc,
DescDeatails: $.buyerDesc
    }
  }
    }
}}}

Expected Output

<?xml version='1.0' encoding='UTF-8'?>
<Order>
  <id>
    <Detail>
      <Line>
        <frame>
          <Desc>d&Bf 5489-lk45 brand apple 13 pieces</Desc>
          <DescDeatails/>
        </frame>
      </Line>
    </Detail>
  </id>
</Order>

Thank you


Solution

  • Having a single ampersand (&) character is invalid in XML. The resulting document would not be able to be parsed in any correct XML parser. DataWeave automatically encodes that character into the right sequence &amp;. No need to add escapeGT or any directive. When an XML parser reads that sequence it will convert it back to a single ampersand. This is the recommended method to use ampersands in XML.

    If for some reason that is not acceptable you can convert that field to a CDATA value where it would be valid to have the single ampersand. That would make sense for values that contain code though I don't see any reason to do so in a description text.

    Example: Desc: $.Desc as CData