I have an XML where I want to find a particular attribute 'type' shiprefnum under ReferenceNums->Reference and replace the value to 124215133_AB_00010.
Input XML:
<?xml version='1.0' encoding='UTF-8'?>
<LeanXML batchref="583628866.tiff" created="201801160501">
<DocumentUpload>
<ShipperIdentificationCode type="DUNS">124215133</ShipperIdentificationCode>
<Document>
<Type>POD</Type>
<Name>583628866.tiff</Name>
<MimeType>image/tiff</MimeType>
<FileData>SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAA</FileData>
<Checksum>7E5CAAF9EC3204798C61B300A0B5DE7FAE106F8C</Checksum>
<Comments>901054881</Comments>
</Document>
**<ReferenceNums>**
<Reference type="customerpo">1593942</Reference>
***<Reference type="shiprefnum">124215133</Reference>***
**</ReferenceNums>**
<Location>
<StopType>Drop</StopType>
<StopNum>2</StopNum>
<LocationRef>NOVAST</LocationRef>
<City>MIDLAND</City>
<State>TX</State>
<Zip>79706</Zip>
<Country>US</Country>
</Location>
</DocumentUpload>
</LeanXML>
Expected Output:
<?xml version='1.0' encoding='UTF-8'?>
<LeanXML batchref="583628866.tiff" created="201801160501">
<DocumentUpload>
<ShipperIdentificationCode type="DUNS">124215133</ShipperIdentificationCode>
<Document>
<Type>POD</Type>
<Name>583628866.tiff</Name>
<MimeType>image/tiff</MimeType>
<FileData>SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAA</FileData>
<Checksum>7E5CAAF9EC3204798C61B300A0B5DE7FAE106F8C</Checksum>
<Comments>901054881</Comments>
</Document>
**<ReferenceNums>**
<Reference type="customerpo">1593942</Reference>
***<Reference type="shiprefnum">124215133_AB_00010</Reference>***
**</ReferenceNums>**
<Location>
<StopType>Drop</StopType>
<StopNum>2</StopNum>
<LocationRef>NOVAST</LocationRef>
<City>MIDLAND</City>
<State>TX</State>
<Zip>79706</Zip>
<Country>US</Country>
</Location>
</DocumentUpload>
</LeanXML>
I tried below dataweave but it didn't work. Can someone help me with this
payload update {
case .LeanXML.DocumentUpload.ReferenceNums.Reference -> if(payload.LeanXML.DocumentUpload.ReferenceNums.Reference.@'type' == "shiprefnum") "124215133_AB_00010" else payload.LeanXML.DocumentUpload.ReferenceNums.Reference
You can use a conditional update in the case and the multi valued selector to find all the child objects in the update expression. This way you don't need an else because objects that don't comply with the condition are ignored by the update operator.
%dw 2.0
output application/xml
var newShiprefnum = "124215133_AB_00010"
---
payload update {
case references at .LeanXML.DocumentUpload.ReferenceNums.*Reference if (references.@"type" == "shiprefnum")
-> newShiprefnum
}
Output:
<?xml version='1.0' encoding='UTF-8'?>
<LeanXML batchref="583628866.tiff" created="201801160501">
<DocumentUpload>
<ShipperIdentificationCode type="DUNS">124215133</ShipperIdentificationCode>
<Document>
<Type>POD</Type>
<Name>583628866.tiff</Name>
<MimeType>image/tiff</MimeType>
<FileData>SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAA</FileData>
<Checksum>7E5CAAF9EC3204798C61B300A0B5DE7FAE106F8C</Checksum>
<Comments>901054881</Comments>
</Document>
<ReferenceNums>
<Reference type="customerpo">1593942</Reference>
<Reference type="shiprefnum">124215133_AB_00010</Reference>
</ReferenceNums>
<Location>
<StopType>Drop</StopType>
<StopNum>2</StopNum>
<LocationRef>NOVAST</LocationRef>
<City>MIDLAND</City>
<State>TX</State>
<Zip>79706</Zip>
<Country>US</Country>
</Location>
</DocumentUpload>
</LeanXML>