Search code examples
xmlmuledataweavemulesoftmule4

xml attribute with value is not getting retained


Input

<?xml version='1.0' encoding='UTF-8'?>
<Requests
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RequestBase type="AccountMaintenanceRequest">
        <Accounts
            xmlns:d8p1="http://schemas.datacontract.org/2004/07/OpenSolutions.CoreApiService.Services.Messages">
            <d8p1:PropertyTax>
                <d8p1:CalendarPeriodCode xsi:nil="true"/>
                <d8p1:TaxOrganizationNumber xsi:nil="true">31072</d8p1:TaxOrganizationNumber>
                <d8p1:AcctPropTaxDetails xsi:nil="true"/>
            </d8p1:PropertyTax>
        </Accounts>
    </RequestBase>
</Requests>

DW without writeNilOnNull=true

%dw 2.0
output application/xml 
ns s http://schemas.xmlsoap.org/soap/envelope/
ns i http://www.w3.org/2001/XMLSchema-instance
ns d6p1 http://schemas.microsoft.com/2003/10/Serialization/Arrays
---
s#Envelope: {
    s#Header: '',
    s#Body: {
        SubmitRequest @(xmlns: "http://www.opensolutions.com/CoreApi"): {
            'input' @("xmlns:i": i): {
                Input: {
                    ExtensionRequests @("xmlns:d6p1": d6p1): '',
                    Requests : {
                        RequestBase @("i:type": payload.Requests.RequestBase.@'type'): payload.Requests.RequestBase
                    }
                }
            }
        }
    }
}

Output without writeNilOnNull=true

<?xml version='1.0' encoding='UTF-8'?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header/>
  <s:Body>
    <SubmitRequest xmlns="http://www.opensolutions.com/CoreApi">
      <input xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Input>
          <ExtensionRequests xmlns:d6p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
          <Requests>
            <RequestBase i:type="AccountMaintenanceRequest">
              <Accounts>
                <d8p1:PropertyTax xmlns:d8p1="http://schemas.datacontract.org/2004/07/OpenSolutions.CoreApiService.Services.Messages">
                  <d8p1:CalendarPeriodCode/>
                  <d8p1:TaxOrganizationNumber/>
                  <d8p1:AcctPropTaxDetails/>
                </d8p1:PropertyTax>
              </Accounts>
            </RequestBase>
          </Requests>
        </Input>
      </input>
    </SubmitRequest>
  </s:Body>
</s:Envelope>

Here in the above scenario, null attributes has been removed from CalendarPeriodCode , TaxOrganizationNumber and AcctPropTaxDetails. Also the value of TaxOrganizationNumber is also lost

Output with writeNilOnNull=true

<?xml version='1.0' encoding='UTF-8'?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header/>
  <s:Body>
    <SubmitRequest xmlns="http://www.opensolutions.com/CoreApi">
      <input xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Input>
          <ExtensionRequests xmlns:d6p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
          <Requests>
            <RequestBase i:type="AccountMaintenanceRequest">
              <Accounts>
                <d8p1:PropertyTax xmlns:d8p1="http://schemas.datacontract.org/2004/07/OpenSolutions.CoreApiService.Services.Messages">
                  <d8p1:CalendarPeriodCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                  <d8p1:TaxOrganizationNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                  <d8p1:AcctPropTaxDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                </d8p1:PropertyTax>
              </Accounts>
            </RequestBase>
          </Requests>
        </Input>
      </input>
    </SubmitRequest>
  </s:Body>
</s:Envelope>

Here in the above scenario, null attributes is there for all fields along with xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace. But still the value of TaxOrganizationNumber is lost.

Expected Output

<?xml version='1.0' encoding='UTF-8'?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header/>
  <s:Body>
    <SubmitRequest xmlns="http://www.opensolutions.com/CoreApi">
      <input xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Input>
          <ExtensionRequests xmlns:d6p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
          <Requests>
            <RequestBase i:type="AccountMaintenanceRequest">
              <Accounts>
                <d8p1:PropertyTax>
                  <d8p1:CalendarPeriodCode xsi:nil="true"/>
                  <d8p1:TaxOrganizationNumber xsi:nil="true">36712</d8p1:TaxOrganizationNumber>
                  <d8p1:AcctPropTaxDetails xsi:nil="true"/>
                </d8p1:PropertyTax>
              </Accounts>
            </RequestBase>
          </Requests>
        </Input>
      </input>
    </SubmitRequest>
  </s:Body>
</s:Envelope>

Maybe Input is incorrect due to nil=true but that is the data which we receive.

  1. Is it possible to keep the original Input fields as it is after mapping without any attribute or values getting removed?
  2. If the above is not possible then with using writeNilOnNull=true is it possible to retain the values atleast?

Since original data is bigger, dynamic solution will help


Solution

  • The attribute xsi:nil is not a custom attribute. It is part of the core XML Schema namespace. xsi:nil="true" means that the element is null. It seems that DataWeave respects that attribute so it returns null when trying to access its content. Probably the behavior is not defined for XML parsers but I would say it is not a good idea to use it in non-null elements.

    I'm not sure the content will be at all accessible after xsi:nil="true" is processed. You may try using a recursive function to filter the attribute based on your previous answer to see if it works.