数据编织2.0中的XML转换 [英] xml transform in data weave 2.0

查看:16
本文介绍了数据编织2.0中的XML转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的示例中,我有一个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自动递增。

推荐答案

我怀疑您试图将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>

这篇关于数据编织2.0中的XML转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆