【问题标题】:xml transform in data weave 2.0数据编织 2.0 中的 xml 转换
【发布时间】:2022-01-20 04:43:28
【问题描述】:

就我而言,我有一个 XML 请求和预期响应负载。但我不知道使用 Dataweave 2.0 进行转换

输入 XML:

<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910" ApplicationNumber="AP-00006354" Version="3.10">
    <QuestionReplies id="QuestionReplies-1553101003-1178947042">
        <QuestionReply id="QuestionReply-859195405-1832325773" Name="1000" Value="NO" VisibleInd="Yes"/>
        <QuestionReply id="QuestionReply-1656171536-493197768" Name="1000A" VisibleInd="No"/>
    </QuestionReplies>
    <DTORisk id="Risk-156119133-1700981150">
        <DTOCoverage>
            <DTOStep id="Step-Coverage-1713637162-341585712-Premium" Status="Cleared"/>
        </DTOCoverage>
    </DTORisk>
    <DTORisk id="Risk-156119133-1700981151">
        <DTOCoverage>
            <DTOStep id="Step-Coverage-1713637162-341585713-Premium" Status="Cleared"/>
        </DTOCoverage>
    </DTORisk>
    <DTOCoverage>
        <DTOStep id="Step-Coverage-1713637162-341585713-Premium" Status="Cleared"/>
    </DTOCoverage>
    <DTOCoverage>
        <DTOStep id="Step-Coverage-1713637162-341585713-Premium" Status="Cleared"/>
    </DTOCoverage>
    <DTOCoverage>
        <DTOStep id="Step-Coverage-1713637162-341585713-Premium" Status="Cleared"/>
    </DTOCoverage>
</DTOApplication>   

输出 XML:

<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910" ApplicationNumber="AP-00006354" Version="3.10">
    <QuestionReplies id="QuestionReplies-1553101003-1178947042">
        <QuestionReply id="QuestionReply-859195405-1832325773" Name="1000" Value="NO" VisibleInd="Yes"/>
        <QuestionReply id="QuestionReply-1656171536-493197768" Name="1000A" VisibleInd="No"/>
    </QuestionReplies>
    <DTORisk id="Risk-156119133-1700981150">
        <DTOCoverage>
            <DTOSteps>
                        <DTOStep Order="1" Name="Final Premium" Factor="501" Value="501"/>
                    </DTOSteps>
        </DTOCoverage>
    </DTORisk>
    <DTORisk id="Risk-156119133-1700981151">
        <DTOCoverage>
            <DTOSteps>
                        <DTOStep Order="1" Name="Final Premium" Factor="502" Value="502"/>
                    </DTOSteps>
        </DTOCoverage>
    </DTORisk>
    <DTOCoverage>
        <DTOSteps>
                <DTOStep Order="1" Name="Final Premium" Factor="503" Value="503"/>
            </DTOSteps>
    </DTOCoverage>
    <DTOCoverage>
        <DTOSteps>
                  <DTOStep Order="1" Name="Final Premium" Factor="504" Value="504"/>
            </DTOSteps>
    </DTOCoverage>
    <DTOCoverage>
        <DTOSteps>
                  <DTOStep Order="1" Name="Final Premium" Factor="505" Value="505"/>
            </DTOSteps>
    </DTOCoverage>
</DTOApplication>   

来源:https://github.com/Manikandan99/jenkins-demo-cicd/blob/master/output_xml

注意:

  • 输入和输出负载的区别在于应该更新 DTOStep 节点的值。
  • DTOStep的属性值每次从500开始自动递增。

【问题讨论】:

  • 预期的输出应该包含在您的问题中,而不是链接。
  • 到底是什么问题?在 Stackoverflow 中,预计您至少尝试过一些东西。如果您不了解该主题,则应先尝试阅读文档,进行一些培训,遵循一些教程,然后尝试,并询问是否有错误。
  • 另外,如果您需要帮助来生成输出,您应该准确解释它与输入的不同之处。

标签: mule dataweave mulesoft mule-component mule4


【解决方案1】:

我怀疑您正在尝试将每个 DTOStep 元素包含在 DTOSteps 父元素中。这可以使用update() 运算符来完成。根据需要转换每个子元素 mapObject() 很有用,因为它还提供了索引。您可能需要针对其他输入微调脚本。

%dw 2.0
output application/xml
var keys=["DTORisk", "DTOCoverage"]
var startingValue=499
fun createOutputElement(keyName, index)=keyName match {
  case "DTOCoverage" -> { DTOCoverage: DTOSteps: DTOStep @(Order:1, Name:"Final Premiun", Factor: index + startingValue, Value: index + startingValue): null }
  case "DTORisk" -> { DTORisk: DTOCoverage: DTOSteps: DTOStep @(Order:1, Name:"Final Premiun", Factor: index + startingValue, Value: index + startingValue): null }
  else -> dw::Runtime::fail("Unexpected key")
}
---
payload update {
        case risk at .DTOApplication ->
            risk mapObject ((value, key, index) -> 
                if (keys contains key as String ) createOutputElement(key as String, index) 
                else (key):value
            )
}

输出:

<?xml version='1.0' encoding='UTF-8'?>
<DTOApplication id="Application-1660258480-1493174910" ApplicationNumber="AP-00006354" Version="3.10">
  <QuestionReplies id="QuestionReplies-1553101003-1178947042">
    <QuestionReply id="QuestionReply-859195405-1832325773" Name="1000" Value="NO" VisibleInd="Yes"/>
    <QuestionReply id="QuestionReply-1656171536-493197768" Name="1000A" VisibleInd="No"/>
  </QuestionReplies>
  <DTORisk>
    <DTOCoverage>
      <DTOSteps>
        <DTOStep Order="1" Name="Final Premiun" Factor="500" Value="500"/>
      </DTOSteps>
    </DTOCoverage>
  </DTORisk>
  <DTORisk>
    <DTOCoverage>
      <DTOSteps>
        <DTOStep Order="1" Name="Final Premiun" Factor="501" Value="501"/>
      </DTOSteps>
    </DTOCoverage>
  </DTORisk>
  <DTOCoverage>
    <DTOSteps>
      <DTOStep Order="1" Name="Final Premiun" Factor="502" Value="502"/>
    </DTOSteps>
  </DTOCoverage>
  <DTOCoverage>
    <DTOSteps>
      <DTOStep Order="1" Name="Final Premiun" Factor="503" Value="503"/>
    </DTOSteps>
  </DTOCoverage>
  <DTOCoverage>
    <DTOSteps>
      <DTOStep Order="1" Name="Final Premiun" Factor="504" Value="504"/>
    </DTOSteps>
  </DTOCoverage>
</DTOApplication>

【讨论】:

  • 是的,更新操作符是正确的。但请参阅输出 xml。 DTOStep 节点不同,请注意 500 的值是自动递增的。
  • 您希望这里的人们如何知道这些细节?请使用转换的详细逻辑更新您的问题,并在问题上查看我的 cmets。
  • 你正确的aled。这是一个接近的电话。如何更新DTOStep节点的属性值?
  • 它有点复杂但可行。当我有更多时间时,我会更新我的答案。请将预期的输出添加到问题中。
  • 更新了脚本并为您的输入添加了输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2018-04-12
相关资源
最近更新 更多