Search code examples
xmlmuledataweavemulesoft

How to remove XML namespace in Mulesoft Dataweave


When I transform a message with Mulesoft dataweave I want to get rid of all the xmlns namespaces.

This is my message:

<?xml version='1.0' encoding='UTF-8'?>
<Entries xmlns="http://www.example.nl/Entries" type="Catalog" name="xxx Catalog" exportDate="2018-01-18T10:08:27.609Z">
  <Entry id="264063" deleted="0" creationDate="2017-05-26T14:26:09.511Z" lastModifiedDate="2017-10-13T22:46:39.000Z">
    <Attributes>
      <Attribute>
        <MetadataPath>Just an example 1</MetadataPath>
        <Locale/>
      </Attribute>
      <Attribute>
        <MetadataPath>Just an example 2</MetadataPath>
        <Locale>en_GB</Locale>
      </Attribute>
    </Attributes>
    <Categories>
      <Category>
        <Hierarchy>GPC_Hierarchy</Hierarchy>
        <Id>999999</Id>
      </Category>
      <Category>
        <Hierarchy>GPC_xx_Hierarchy</Hierarchy>
        <Id>999998</Id>
      </Category>
    </Categories>
    <Specs>
      <Spec>Validatie Spec</Spec>
      <Spec>Item Spec</Spec>
    </Specs>
  </Entry>
</Entries>

When I run this dataweave code:

%dw 2.0
var x = payload.Entries
output application/xml encoding="utf-8"
---

{
   Entry @('type': x.@'type', name: x.@name, exportDate: x.@exportDate, id: x.Entry.@id, 
  deleted: x.Entry.@deleted, creationDate: x.Entry.@creationDate, lastModifiedDate:    x.Entry.@lastModifiedDate )
    : {(x.Entry.Attributes ), 
    (
       Categories: (x.Entry.Categories)
    ),
    (
      Specs:  (x.Entry.Specs)
    )
    }
    
    
}

Then this is the result

<?xml version='1.0' encoding='UTF-8'?>
<Entry type="Catalog" name="xxx Catalog" exportDate="2018-01-18T10:08:27.609Z" id="264063" deleted="0" creationDate="2017-05-26T14:26:09.511Z" lastModifiedDate="2017-10-13T22:46:39.000Z">
  <Attribute xmlns="http://www.example.nl/Entries">
    <MetadataPath>Just an example 1</MetadataPath>
    <Locale/>
  </Attribute>
  <Attribute xmlns="http://www.example.nl/Entries">
    <MetadataPath>Just an example 2</MetadataPath>
    <Locale>en_GB</Locale>
  </Attribute>
  <Categories>
    <Category xmlns="http://www.example.nl/Entries">
      <Hierarchy>GPC_Hierarchy</Hierarchy>
      <Id>999999</Id>
    </Category>
    <Category xmlns="http://www.example.nl/Entries">
      <Hierarchy>GPC_xx_Hierarchy</Hierarchy>
      <Id>999998</Id>
    </Category>
  </Categories>
  <Specs>
    <Spec xmlns="http://www.example.nl/Entries">Validatie Spec</Spec>
    <Spec xmlns="http://www.example.nl/Entries">Item Spec</Spec>
  </Specs>
</Entry>

How to get rid of all those xmlns="http://www.example.nl/Entries" namespaces ?

Off course I can rewrite the message but that is not my intention. Mabye there is some kind of tag in dataweave something like this ?

output application/xml removeAllNamespaces

thanks


Solution

  • You can try below code which meets your expectation . There is no direct writer property for XML to remove all the nameSpace.

    %dw 2.0
    var x = payload.Entries
    
    fun removeALLNameSpacesFromXML(in) =
      in mapObject {
        '$$' @(($$.@)): 
          if ($ is Object)
            removeALLNameSpacesFromXML($)
          else
            ($)
      }
      //This is your transormation code ,I have just assigned with some variable to pass as a Function argument of removeALLNameSpacesFromXML function. 
    var yourPayloadWithXmlns = {
      Entry @('type': x.@'type', name: x.@name, exportDate: x.@exportDate, id: x.Entry.@id, deleted: x.Entry.@deleted, creationDate: x.Entry.@creationDate, lastModifiedDate: x.Entry.@lastModifiedDate): {
        (x.Entry.Attributes),
        (
          Categories: (x.Entry.Categories)
        ),
        (
          Specs: (x.Entry.Specs)
        )
      }
    }
    output application/xml  
    ---
    removeALLNameSpacesFromXML(yourPayloadWithXmlns)
    

    Sample Output:

    <?xml version='1.0' encoding='UTF-8'?>
    <Entry type="Catalog" name="xxx Catalog" exportDate="2018-01-18T10:08:27.609Z" id="264063" deleted="0" creationDate="2017-05-26T14:26:09.511Z" lastModifiedDate="2017-10-13T22:46:39.000Z">
      <Attribute>
        <MetadataPath>Just an example 1</MetadataPath>
        <Locale/>
      </Attribute>
      <Attribute>
        <MetadataPath>Just an example 2</MetadataPath>
        <Locale>en_GB</Locale>
      </Attribute>
      <Categories>
        <Category>
          <Hierarchy>GPC_Hierarchy</Hierarchy>
          <Id>999999</Id>
        </Category>
        <Category>
          <Hierarchy>GPC_xx_Hierarchy</Hierarchy>
          <Id>999998</Id>
        </Category>
      </Categories>
      <Specs>
        <Spec>Validatie Spec</Spec>
        <Spec>Item Spec</Spec>
      </Specs>
    </Entry>