如何使用 Mule 4 删除 XML 中返回 null 的属性?

发布于 2025-01-10 11:27:04 字数 2194 浏览 0 评论 0原文

我需要使用 dataweave 2.0 从 XML 中删除“null”属性,

我尝试了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 删除属性返回“null”的任何想法2.0?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2025-01-17 11:27:04

尝试下面的 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文