My Input Xml looks like below
<?xml version="1.0" encoding="UTF-8"?>
<elements>
<element>
<id>123</id>
<items>
<item>
<attributes>
<attribute attribute-id="test1">attr1</attribute>
<attribute attribute-id="test2">attr2</attribute>
</attributes>
</item>
</items>
</element>
</elements>
I tried with below Dataweave script:
%dw 1.0
%output application/xml
---
{
elements: { (payload.elements.*element map {
element: {
test1 : $.items.item.attributes.attribute when $.items.item.attributes.attribute.@attribute-id == 'test1' otherwise '',
test2 : $.items.item.attributes.attribute when $.items.item.attributes.attribute.@attribute-id == 'test2' otherwise ''
}
}
)
}
}
Expected output:
<?xml version='1.0' encoding='windows-1252'?>
<elements>
<element>
<test1>attr1</test1>
<test2>attr2</test2>
</element>
</elements>
Actual output:
<?xml version='1.0' encoding='windows-1252'?>
<elements>
<element>
<test1>attr1</test1>
<test2></test2>
</element>
</elements>
What changes are needed in my data weave script to get the expected output?