In my case, I have an XML Request and Expected response payload. But I don't know to transform using Dataweave 2.0
Input 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>
Output 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>
source: https://github.com/Manikandan99/jenkins-demo-cicd/blob/master/output_xml
Note:
I suspect that you are trying to enclose the DTOStep
elements each into a DTOSteps
parent element. That can be done using the update() operator. To transform each child element as needed mapObject() is useful as it also provides the index. You may need to finetune the script for other inputs.
%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
)
}
Output:
<?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>