Search code examples
muledataweave

Remove attributes and values from XML when it matches the given key


I have to remove a few fields from the XML payload. I wrote a function below but it is checks and removes only the keys.

How to apply the condition also to attributes and values?

Basically, it has remove whereever the fields are available like tag/value/attributes

Sample Input

%dw 1.0
%output application/xml skipNullOn="everywhere"
%function remove(content, filterList)
    content match {
        :array -> $ map (value,index) -> remove(value, filterList),
        :object -> $ mapObject (value,key) ->
            (key) @((key.@)) : null when (filterList contains key as :string)
                otherwise remove(value, filterList)
            ,
        default -> content
    }
---
remove(payload, ["userId","companyId","account"])

Solution

  • Input is not a valid XML, I have updated to make it valid. The following Dataweave removes all attribute name/value and tag/value matching the filter list.

    Note that in the output attribute type is removed because the value matches account

    Input

    <?xml version='1.0' encoding='UTF-8'?>
    <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:test="http://host.com2003/05/soap-envelope" xmlns:ns6="http://host.com/V001">
        <soapenv:Header>
            <axis2ns11343:OSARequestHeader xmlns:axis2ns11343="http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001" soapenv:mustUnderstand="false">
                <axis2ns11343:userId>e2eauto01a</axis2ns11343:userId>
                <axis2ns11343:companyId>test</axis2ns11343:companyId>
                <axis2ns11343:companyType/>
                <axis2ns11343:clientMachineId>1.1.1.1</axis2ns11343:clientMachineId>
            </axis2ns11343:OSARequestHeader>
            <axis2ns11344:cpsMessageHeader xmlns:axis2ns11344="http://host.com/xsd/types/V002" type="currency" guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"/>
        </soapenv:Header>
        <soapenv:Body>
            <ns8:getCurrencyProfileRequest xmlns:ns8="http://host.com/xsd/service/V001" accType ="code" username="username" type="account">
                <ns8:userInfo>
                    <ns3:type xmlns:ns3="http://host.com/xsd/commontypes/V001">accountNo</ns3:type>
                    <ns3:companyId xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:companyId>
                    <ns3:userId xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:userId>
                    <ns3:accountNo xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:accountNo>
                    <ns3:txid xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:txid>
                </ns8:userInfo>
            </ns8:getCurrencyProfileRequest>
        </soapenv:Body>
    </soapenv:Envelope>
    
    %dw 1.0
    %output application/xml skipNullOn="everywhere"
    
    %function checkAndRemoveAttributes (attribs, filterList)
     (attribs default {}) mapObject {
        ($$): null when (filterList contains ($$ as :string)) otherwise $
     }
      
    %function remove(content, filterList)
        content match {
            :object -> $ mapObject (value,key) ->
                (key) @((checkAndRemoveAttributes(key.@,filterList))): null when (filterList contains key as :string)
                    otherwise remove(value, filterList)
                ,
            default -> null when (filterList contains content as :string) otherwise content
        }
    ---
    remove(payload, ["userId","companyId","account","username", "code"])
    

    Output

    <?xml version='1.0' encoding='US-ASCII'?>
    <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
      <soapenv:Header>
        <axis2ns11343:OSARequestHeader xmlns:axis2ns11343="http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001" soapenv:mustUnderstand="false">
          <axis2ns11343:companyType></axis2ns11343:companyType>
          <axis2ns11343:clientMachineId>1.1.1.1</axis2ns11343:clientMachineId>
        </axis2ns11343:OSARequestHeader>
        <axis2ns11344:cpsMessageHeader xmlns:axis2ns11344="http://host.com/xsd/types/V002" type="currency" guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"></axis2ns11344:cpsMessageHeader>
      </soapenv:Header>
      <soapenv:Body>
        <ns8:getCurrencyProfileRequest xmlns:ns8="http://host.com/xsd/service/V001" accType="code" type="account">
          <ns8:userInfo>
            <ns3:type xmlns:ns3="http://host.com/xsd/commontypes/V001">accountNo</ns3:type>
            <ns3:accountNo xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:accountNo>
            <ns3:txid xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:txid>
          </ns8:userInfo>
        </ns8:getCurrencyProfileRequest>
      </soapenv:Body>
    </soapenv:Envelope>