1

In my case, I'm trying to update the DTOSteps (first child node) values of @CoverageCd == "TRIA".

But my dataweave code updates all the DTOSteps of @CoverageCd == "TRIA". Assume that the variable in this code returns some values.

My Dataweave code:

%dw 2.0
var multiChar = "X"
output application/xml

fun TRIAtransformSteps(x, index)=
    x  match {
      case is Object -> x mapObject 
        if  ($$ as String == "DTOSteps")
        {
                    DTOSteps: TRIAtransformNewSteps() 
        }
        else 
            (($$): TRIAtransformSteps($, index+1)) 
      else -> $
}
    
fun TRIAtransformNewSteps()=
    {
        DTOStep @(Order:vars.Desc[0], Name:"Rate Area: "++ vars.Desc ++ " TRIA",Desc:vars.Desc ++ " TRIA Layer Premium", Operation:"+", Factor: vars.drcvalues.TRIACharge , Value:vars.drcvalues.TRIACharge ): null
    }
    
fun TRIAtransformCoverage(x, index)=
    x match {
      case is Object -> x mapObject 
        if ($$ as String == "DTOCoverage" and $$.@CoverageCd == "TRIA")
            { 
                DTOCoverage @(( $$.@ ) ): TRIAtransformSteps($, index)
            }
        else 
            (($$): TRIAtransformCoverage($, index+1)) 
      else -> $
    }
---
TRIAtransformCoverage(payload,1)

Input payload: https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/input_xml_request_TRIA.xml

Output in getting : https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/My_output.xml

Expected Output : https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/Expected_output.xml

How do I break the iteration when the First DTOSteps is selected?

codey_08
  • 249
  • 1
  • 11

2 Answers2

3

If the element is the first child of their parent element, then you can use the third parameter of the mapObject() and filter object() functions, the index. Often forgotten, they have three parameters: (value,key,index). If not named it can be referenced as $$$ by default. You can add a condition to check if it is the first child.

For this case you could use filterObject() to retain only the desired key if index is the first on (ie index==0).

The condition could be for example:

x filterObject ($$ as String == "DTOSteps" and $$$ == 0) 
mapObject if ..
aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks for your answer. How do I add that condition? Could you please update that line in my dataweave? – codey_08 Apr 30 '22 at 14:03
  • I have updated my answer because filterObject is a better match. I added an example. Having said that, you should not expect answers here to give you the fixed code. In Stackoverflow answers should provide the method or suggestion to fix the issue. Applying the solution is your job. – aled Apr 30 '22 at 16:54
2

Please provide the values of any variables used. To get the output, You need to change the TRIAtransformSteps function like below

fun TRIAtransformSteps(x, index)=
    x  match {
      case is Object -> x mapObject 
        if  ($$ as String == "DTOSteps")
        {
            DTOSteps: TRIAtransformNewSteps() 
        }
        else 
            (($$): $)
      else -> $
}
sudhish_s
  • 573
  • 2
  • 5