如何使用 Mule 4 删除 XML 中 returns null 的属性?
How to remove the attribute that returns null in XML using Mule 4?
我需要使用 dataweave 2.0从 XML 中删除“空”属性,
我试过 skipNullOn="everywhere" 并且 skipOnNull="attribute" 只对元素有效,对属性无效。
使用 skipNullOn 时出错:
"Option `skipOnNull` is not valid. Valid options are: `writeDeclaration`, `indent`, `doubleQuoteInDeclaration`, `encoding`, `escapeGT`, `defaultNamespace`, `bufferSize`, `inlineCloseOn`, `onInvalidChar`, `writeNilOnNull`, `escapeCR`, `skipNullOn`, `writeDeclaredNamespaces`, `deferred`
输入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>
预期XML输出:https://github.com/Manikandan99/Map_request/blob/main/null_removed.xml
关于如何使用 dataweave 2.0 删除属性 return“null”有什么想法吗?
试试下面的 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)
输出
<?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>
我需要使用 dataweave 2.0从 XML 中删除“空”属性,
我试过 skipNullOn="everywhere" 并且 skipOnNull="attribute" 只对元素有效,对属性无效。
使用 skipNullOn 时出错:
"Option `skipOnNull` is not valid. Valid options are: `writeDeclaration`, `indent`, `doubleQuoteInDeclaration`, `encoding`, `escapeGT`, `defaultNamespace`, `bufferSize`, `inlineCloseOn`, `onInvalidChar`, `writeNilOnNull`, `escapeCR`, `skipNullOn`, `writeDeclaredNamespaces`, `deferred`
输入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>
预期XML输出:https://github.com/Manikandan99/Map_request/blob/main/null_removed.xml
关于如何使用 dataweave 2.0 删除属性 return“null”有什么想法吗?
试试下面的 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)
输出
<?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>