0

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

Karthik
  • 2,181
  • 4
  • 10
  • 28

1 Answers1

1

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.

aled
  • 21,330
  • 3
  • 27
  • 34
  • So instead of xsi:nil = true if it was i:nil=true, a custom attribute then anything can be done? @aled – Karthik Feb 16 '23 at 17:08
  • If it is a custom attributes the attribute and the element content will be propagated to the output. – aled Feb 16 '23 at 17:58
  • Was the answer useful? – aled Feb 19 '23 at 13:22
  • Ya basically . I couldn't do anything directly due to namespace behaviour like u mentioned.. have to write recursive function to maintain the values @aled – Karthik Feb 19 '23 at 17:21
  • 1
    I would say that there is a problem with the input being inconsistent. – aled Feb 19 '23 at 17:31