Как выполнить цикл внутри цикла для сопоставления с CSV из XML

<?xml version="1.0"?>
<tXML>
  <Header>
    <Source>1</Source>
    <Action_Type>Update</Action_Type>
    <Sequence_Number>0</Sequence_Number>
    <Batch_ID>4</Batch_ID>
    <Reference_ID>043</Reference_ID>
    <User_ID>S</User_ID>
    <Password>password</Password>
    <Message_Type></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>
      <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>
      <LineItem>
        <DoLineNbr>2</DoLineNbr>
        <ItemName>4550155140404</ItemName>
        <Description>AS LAMBSWOOL VNECK</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>

Мой код выглядит так

%dw 2.0
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: "",
})

Итак, в приведенном выше коде я зацикливаюсь на каждом элементе строки, чтобы получить поля, но внутри каждого элемента строки я должен цикл для каждого комментария и для каждого комментария, который мне нужно поместить в другую строку. Например, вывод должен выглядеть так, как вы видите в примере, если в позиции 2 есть 3 раздела комментариев, у меня есть 3 строки для 3 комментариев.

00000002|0001333006|20191220|34621|SAPTOMIF4|CommentText1|||6500055464123|252||J1||||MW09449| 
00000002|0001333006|20191220|34621|SAPTOMIF4|CommentText2|||6500055464123|252||J1||||MW09449| 
00000002|0001333006|20191220|34621|SAPTOMIF4|CommentText3|||6500055464123|252||J1||||MW09449|  
000000003|0001333006|20191220|34621|SAPTOMIF6|CommentText1|||6500055654123|4542||J1||||MW09449|
000000003|0001333006|20191220|34621|SAPTOMIF6|CommentText2|||6500055654123|4542||J1||||MW09449|
000000003|0001333006|20191220|34621|SAPTOMIF6|CommentText3|||6500055654123|4542||J1||||MW09449|
000000004|0001333006|20191220|34621|SAPTOMIF6||||65000545664123|35463||J1||||MW09449| 

person Nag    schedule 10.01.2020    source источник
comment
Sry Мой код выглядит так, как будто этот зацикленный уровень элемента строки ``% dw 2.0 output application / csv header = false, separator = | , quoteValues ​​= false payload.tXML.Message.DistributionOrder. * Карта LineItem ((LineItem, indexofLineItem) - ›{column_1: 000000001, column_2: 0, column_3: 0, column_4: (LineItem.LineProcessInfo.LineRefTextField2 splitBy ) [ 0] заменить [на значение по умолчанию, column_5: (LineItem.LineProcessInfo.LineRefTextField2 splitBy) [1] заменить [на значение по умолчанию, column_6: ???? column_7:, column_8:, column_9:, column_10:,}) ` ``   -  person Nag    schedule 10.01.2020
comment
Привет, я попытался немного отформатировать ваш код для удобства чтения. Просмотрите и при необходимости отредактируйте. Вы можете добавить языковой тег, чтобы выделить синтаксис вашего кода. Пожалуйста, также переместите свой комментарий в вопрос; код не читается в комментариях. Или удалите комментарий, если он устарел. См. Также Как задать вопрос.   -  person Robert    schedule 10.01.2020


Ответы (1)


Вы можете попробовать выполнить карту внутри карты. Однако вам нужно сгладить полученный объект, если вы хотите, чтобы он был записан как csv, поскольку он приведет к массиву внутри массива.

См. Пример кода ниже:

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

var payload = read("<tXML>
      <Header>
        <Source>1</Source>
        <Action_Type>Update</Action_Type>
        <Sequence_Number>0</Sequence_Number>
        <Batch_ID>4</Batch_ID>
        <Reference_ID>043</Reference_ID>
        <User_ID>S</User_ID>
        <Password>password</Password>
        <Message_Type></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>
          <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>
          <LineItem>
            <DoLineNbr>2</DoLineNbr>
            <ItemName>4550155140404</ItemName>
            <Description>AS LAMBSWOOL VNECK</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>", "application/xml")
---

flatten(payload.tXML.Message.DistributionOrder.*LineItem map (LineItem) -> 
    LineItem.*Comment map (Comment) ->
    {
        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: Comment.CommentText, 
        column_7: "", 
        column_8: "", 
        column_9: "", 
        column_10: ""
    })

Это приведет к

000000001|0|0|||[JPY_ _Mens_ _ _ _ _ _ _ _ _ _ _ ]||||
000000001|0|0|||[ _00016000.00000_.00000_.00000_ _ _ _ _ _ _ _ ]||||
000000001|0|0|||[ _ _ _ _R]||||
000000001|0|0|||[JPY_ _Mens_ _ _ _ _ _ _ _ _ _ _ ]||||
000000001|0|0|||[ _00016000.00000_.00000_.00000_ _ _ _ _ _ _ _ ]||||
000000001|0|0|||[ _ _ _ _R]||||
person oim    schedule 10.01.2020
comment
Не могли бы вы также проверить и этот вопрос stackoverflow.com/questions/59720332/ - person Nag; 13.01.2020