Mapping from XML to CSV loop over values separated by underscore

[亡魂溺海] 提交于 2020-01-16 09:59:50

问题


<tXML>
  <Header>
    <Source>J1_RETAIL</Source>
    <Action_Type>Update</Action_Type>
    <Sequence_Number>0</Sequence_Number>
    <Batch_ID>4383352</Batch_ID>
    <Reference_ID>04381645</Reference_ID>
    <User_ID>SAP</User_ID>
    <Password>password</Password>
    <Message_Type>SAP_DO</Message_Type>
    <Company_ID>J1</Company_ID>
    <Msg_Locale>English (United States)</Msg_Locale>
    <Msg_Time_Zone>Eastern Standard Time</Msg_Time_Zone>
    <Version></Version>
    <Internal_Reference_ID></Internal_Reference_ID>
    <Internal_Date_Time_Stamp></Internal_Date_Time_Stamp>
    <External_Reference_ID></External_Reference_ID>
    <External_Date_Time_Stamp></External_Date_Time_Stamp>
  </Header>
  <Message>
    <DistributionOrder>
        <ProcessInfo>
            <RefTextField1></RefTextField1>
            <RefTextField2></RefTextField2>
            <RefTextField3>S082</RefTextField3>
            <RefTextField4></RefTextField4>
            <RefTextField5></RefTextField5>
            <RefTextField6></RefTextField6>
            <RefTextField7>J1</RefTextField7>
            <RefTextField8>[0001333006_SAPTOMIF]</RefTextField8>
            <RefTextField9></RefTextField9>
            <RefTextField10>[ _20191223]</RefTextField10>
            <RefNumberField1>20191220</RefNumberField1>
            <RefNumberField2>34621</RefNumberField2>
            <RefNumberField3></RefNumberField3>
            <RefNumberField4>53</RefNumberField4>
            <RefNumberField5>13</RefNumberField5>
        </ProcessInfo>
        <Comment>
            <NoteType>MB</NoteType>
            <NoteCode>05</NoteCode>
            <CommentText>[00000_ _ _ _ _ _ _ ]</CommentText>
            <Visibility>0</Visibility>
          </Comment>
          <CustomFieldList>
            <CustomField>
              <Name>SiteID</Name>
              <Value></Value>
            </CustomField>
        <LineItem>
            <DoLineNbr>1</DoLineNbr>
            <ItemName>135465</ItemName>
            <Description>A</Description>
            <UpdateActionType></UpdateActionType>
            <PackageType></PackageType>
            <DoLineStatus>Released</DoLineStatus>
            <InventoryAttributes>
              <InventoryType>F</InventoryType>
              <ProductStatus></ProductStatus>
              <BatchNbr></BatchNbr>
              <CountryOfOrigin></CountryOfOrigin>
              <ItemAttribute1>R</ItemAttribute1>
              <ItemAttribute2></ItemAttribute2>
              <ItemAttribute3></ItemAttribute3>
              <ItemAttribute4></ItemAttribute4>
              <ItemAttribute5></ItemAttribute5>
            </InventoryAttributes>
          <Comment>
            <NoteType>MB</NoteType>
            <NoteCode>20</NoteCode>
            <CommentText>[JPY_ _Mens_ _ _ _ _ _ _ _ _ _ _ ]</CommentText>
            <Visibility>0</Visibility>
          </Comment>
          <Comment>
            <NoteType>MB</NoteType>
            <NoteCode>13</NoteCode>
            <CommentText>[ _00016000.00000_.00000_.00000_ _ _ _ _ _ _ _ ]</CommentText>
            <Visibility>0</Visibility>
          </Comment>
          <Comment>
            <NoteType>SC</NoteType>
            <NoteCode>02</NoteCode>
            <CommentText>[ _ _ _ _R]</CommentText>
            <Visibility>0</Visibility>
          </Comment>
        </LineItem>
    </DistributionOrder>
  </Message>
</tXML> 

I'm mapping above XMl to CSV pipe de-limited format and need help on how to loop and print CommentText value.

My code looks like this

output application/csv header = false , separator = "|" , quoteValues = false
---
payload.tXML.Message.*DistributionOrder map ((DistributionOrder , indexofDistributionOrder) ->  {
    column_1: "000000001",
    column_2: "0",
    column_3: "0",
    column_4: (LineItem.LineProcessInfo.LineRefTextField2 splitBy "_")[0] replace "[" with "" default "",
    column_5: (LineItem.LineProcessInfo.LineRefTextField2 splitBy "_")[1] replace "[" with "" default "",
    column_6: ????
    column_7: "",
    column_8: "",
    column_9: "",
    column_10: "",
})

In the above code i need to check for condition if NoteType = MB and NoteCode =05 and I have to loop over each comment text and under DistributionOrder to get the values from [00000_ 1_2_3_4_5_6_7] I need to put each value (for example 1,2,3...7) in each line.

00000001|0001333006|20191220|34621|SAPTOMIF4|CommentText1|||6500055464123|252||J1||||MW09449| 
00000001|0001333006|20191220|34621|SAPTOMIF4|CommentText2|||6500055464123|252||J1||||MW09449| 
00000001|0001333006|20191220|34621|SAPTOMIF4|CommentText3|||6500055464123|252||J1||||MW09449|  
00000001|0001333006|20191220|34621|SAPTOMIF6|CommentText4|||6500055654123|4542||J1||||MW09449|
00000001|0001333006|20191220|34621|SAPTOMIF6|CommentText5|||6500055654123|4542||J1||||MW09449|
00000001|0001333006|20191220|34621|SAPTOMIF6|CommentText6|||6500055654123|4542||J1||||MW09449|
00000001|0001333006|20191220|34621|SAPTOMIF6|CommentText7|||6500055654123|4542||J1||||MW09449|

回答1:


Code below will use combination of flatMap (instead of map and flatten) and splitBy

%dw 2.0
output application/csv header = false , separator = "|" , quoteValues = false

var payload = read("<tXML>
      <Message>
        <DistributionOrder>
          <LineItem>
            <Comment>
              <NoteType>MB</NoteType>
              <NoteCode>20</NoteCode>
              <CommentText>[JPY_ _Mens_ _ _ _ _ _ _ _ _ _ _ ]</CommentText>
              <Visibility>0</Visibility>
            </Comment>
            <Comment>
              <NoteType>MB</NoteType>
              <NoteCode>13</NoteCode>
              <CommentText>[ _00016000.00000_.00000_.00000_ _ _ _ _ _ _ _ ]</CommentText>
              <Visibility>0</Visibility>
            </Comment>
          </LineItem>
          <LineItem>
            <Comment>
              <NoteType>MB</NoteType>
              <NoteCode>20</NoteCode>
              <CommentText>[JPY_ _Mens_ _ _ _ _ _ _ _ _ _ _ ]</CommentText>
              <Visibility>0</Visibility>
            </Comment>
            <Comment>
              <NoteType>MB</NoteType>
              <NoteCode>13</NoteCode>
              <CommentText>[ _00016000.00000_.00000_.00000_ _ _ _ _ _ _ _ ]</CommentText>
              <Visibility>0</Visibility>
            </Comment>
            <Comment>
              <NoteType>SC</NoteType>
              <NoteCode>02</NoteCode>
              <CommentText>[ _ _ _ _R]</CommentText>
              <Visibility>0</Visibility>
            </Comment>
            <Comment>
              <NoteType>MB</NoteType>
              <NoteCode>05</NoteCode>
              <CommentText>1_2_3_4_ _6</CommentText>
              <Visibility>0</Visibility>
            </Comment>
            </LineItem>
        </DistributionOrder>
      </Message>
    </tXML>", "application/xml")

fun outputMap (LineItem, column6="") = 
    {
            column_1: "000000001", 
            column_2: "0", 
            column_3: "0", 
            column_4: (LineItem.LineProcessInfo.LineRefTextField2 splitBy "_")[0] replace "[" with "" default "", 
            column_5: (LineItem.LineProcessInfo.LineRefTextField2 splitBy "_")[1] replace "[" with "" default "", 
            column_6: column6 default "", 
            column_7: "", 
            column_8: "", 
            column_9: "", 
            column_10: ""
    }
---

payload.tXML.Message.DistributionOrder.*LineItem flatMap (LineItem) -> 
    using (filteredComment = LineItem.*Comment[?($.NoteType == "MB" and $.NoteCode == "05")])
    if (filteredComment != null) 
        filteredComment flatMap(commentObject) -> 
            using (splitCommentText = commentObject.CommentText splitBy /_/)
            splitCommentText flatMap (commentText) -> outputMap(LineItem, commentText)
    else 
        outputMap(LineItem)

This will result to

000000001|0|0|||||||
000000001|0|0|||1||||
000000001|0|0|||2||||
000000001|0|0|||3||||
000000001|0|0|||4||||
000000001|0|0||| ||||
000000001|0|0|||6||||

Note that I have updated the payload to display the comments based on the given condition. Sequence of the above code will loop through LineItems -> pick the comments given the condition -> loop through the selected comments -> split the comment text -> iterate over the resulting array. I have added an if else statement here to check if the selected/filtered comment object exists.

Update:

splitCommentText[?($$>0)] flatMap (commentText) -> outputMap(LineItem, commentText)

Added filter to consider only the values with index > 0.



来源:https://stackoverflow.com/questions/59720332/mapping-from-xml-to-csv-loop-over-values-separated-by-underscore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!