mulemule-studiodataweavemulesoftmule-component

How to remove namespaces from xml payload


I want to remove all the namespaces from my XML input.

Input payload

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.mycompany/2015/07">
  <soap:Header/>
  <soap:Body>
    <ns1:GetVehDetails/>
  </soap:Body>
</soap:Envelope>

required output:

<?xml version='1.0' encoding='UTF-8'?>
<Envelope >
  <Header/>
  <Body>
    <GetVehDetails/>
  </Body>
</Envelope>

Solution

  • Based on previous answer https://stackoverflow.com/a/71022382/721855 this function removes all namespaces without the need to specify one.

    %dw 2.0
    output application/xml
    fun removeAllNamespace(element) =
      element mapObject (value, key) -> 
                 ((key as String)) @((key.@)) : (
                    if (value is Object) removeAllNamespace(value)                                  else value
                )
    ---
    removeAllNamespace(payload)