最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dataweave - How to loop through the attributes inside the XML and update its value? - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question edited Jan 29 at 7:31 user12277274 asked Jan 29 at 5:36 user12277274user12277274 1113 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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>
发布评论

评论列表(0)

  1. 暂无评论