1

I need to remove "null" attributes from an XML using dataweave 2.0,

I tried skipNullOn="everywhere" and skipOnNull="attribute" is working only on elements not on attributes.

Error on using skipNullOn :

"Option `skipOnNull` is not valid. Valid options are: `writeDeclaration`, `indent`, `doubleQuoteInDeclaration`, `encoding`, `escapeGT`, `defaultNamespace`, `bufferSize`, `inlineCloseOn`, `onInvalidChar`, `writeNilOnNull`, `escapeCR`, `skipNullOn`, `writeDeclaredNamespaces`, `deferred`

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910" ApplicationNumber="AP-00006354">
    <DTOLine id="Line-101746978-972142087" StatusCd="Active" >
        <DTOCoverage id="Coverage-1713637162-341585712" CoverageCd="MINP" >
            <DTOSteps>
                <DTOStep Order="1" Name="Premium" value="8" factor="8"/>
                <DTOStep Order="2" Name="Term Factor" value="9" factor="9">
                    <DTOSteps>
                        <DTOStep Order="1" Name="Minimum Premium" value="10" factor="10"/>
                        <DTOStep Order="2" Name="Rated Premium" value="null" factor="null"/>
                    </DTOSteps>
                </DTOStep>
            </DTOSteps>
        </DTOCoverage>
        <DTOCoverage id="Coverage-763105832-915106268" CoverageCd="TRIAL">
            <DTOSteps id="Steps-Coverage-763105832-915106268">
                <DTOStep Order="1" Name="Premium" value="18" factor="18" />
            </DTOSteps>
        </DTOCoverage>
    </DTOLine>
    <DTOCoverage id="Coverage-863105832-915106268" CoverageCd="EBRK" >
        <DTOSteps id="Steps-Coverage-863105832-915106268">
            <DTOStep Order="1" Name="Base Rate" value="null" factor="null"/>
        </DTOSteps>
    </DTOCoverage>
</DTOApplication>

Expected XML Output : https://github.com/Manikandan99/Map_request/blob/main/null_removed.xml

Any ideas please on how to remove attribute return "null" using dataweave 2.0?

codey_08
  • 249
  • 1
  • 11
  • did you try by converting it to json first and remove all fields that is giving null value and convert back to xml? also given inpit xml is not valid XML please check once – Anurag Sharma Feb 28 '22 at 07:29
  • Hi, Anurag Sharma, I tried skipOnNull="attribute" in json and then converted to xml, but it removed all attribute values instead of removing only the specific attribute returning null. – codey_08 Feb 28 '22 at 07:39
  • Updated the Input XML ... – codey_08 Feb 28 '22 at 07:52

1 Answers1

3

Try the below DW

DW

%dw 2.0
output application/xml
var updateval = (element) ->
  element mapObject (value, key) -> {
    (key) @(
        (
          if (key.@?)
            (key.@ mapObject ((avalue, akey) -> { 
                    ((akey): avalue) if (avalue != "null")
                }))
          else {}
        )
    ) :
    if (value is Object)
      updateval(value)
    else value
  }
---
updateval(payload)

Output

<?xml version='1.0' encoding='UTF-8'?>
<DTOApplication id="Application-1660258480-1493174910" ApplicationNumber="AP-00006354">
  <DTOLine id="Line-101746978-972142087" StatusCd="Active">
    <DTOCoverage id="Coverage-1713637162-341585712" CoverageCd="MINP">
      <DTOSteps>
        <DTOStep Order="1" Name="Premium" value="8" factor="8"/>
        <DTOStep Order="2" Name="Term Factor" value="9" factor="9">
          <DTOSteps>
            <DTOStep Order="1" Name="Minimum Premium" value="10" factor="10"/>
            <DTOStep Order="2" Name="Rated Premium"/>
          </DTOSteps>
        </DTOStep>
      </DTOSteps>
    </DTOCoverage>
    <DTOCoverage id="Coverage-763105832-915106268" CoverageCd="TRIAL">
      <DTOSteps id="Steps-Coverage-763105832-915106268">
        <DTOStep Order="1" Name="Premium" value="18" factor="18"/>
      </DTOSteps>
    </DTOCoverage>
  </DTOLine>
  <DTOCoverage id="Coverage-863105832-915106268" CoverageCd="EBRK">
    <DTOSteps id="Steps-Coverage-863105832-915106268">
      <DTOStep Order="1" Name="Base Rate"/>
    </DTOSteps>
  </DTOCoverage>
</DTOApplication>
Karthik
  • 2,181
  • 4
  • 10
  • 28