-2

I need a DataWeave 2.0 script that complies with the following rules.
Rules:

  • BID for PtID Z001 will map to Bpay Tag.
  • BID for PtID Z002 will map to Payment Tag.
  • etc

Input XML File:

<File>
    <Block>BlockID</Block>
    <Identification actionCode="04">
        <PtID>Z001</PtID>
        <BID>BPayID</BID>
        <BText>Partner ID for Z001 Section</BText>
    </Identification>
    <Identification actionCode="04">
        <PtID>Z002</PtID>
        <BID>PaymentID</BID>
        <BText>Partner ID for Z002 Section</BText>
    </Identification>
    <Identification actionCode="04">
        <PtID>Z003</PtID>
        <BID>CreditID</BID>
        <BText>Partner ID for Z003 Section</BText>
    </Identification>
    <Identification actionCode="04">
        <PtID>Z004</PtID>
        <BID>DebitID</BID>
        <BText>Partner ID for Z004 Section</BText>
    </Identification>
    <Identification actionCode="04">
        <PtID>Z005</PtID>
        <BID>TaxID</BID>
        <BText>Partner ID for Z005 Section</BText>
    </Identification>
</File>

Expected XML Output:

<Result>
    <Block>BlockID</Block>
    <Bpay>BPayID</Bpay>
    <Payment>PaymentID</Payment>
    <Credit>CreditID</Credit>
    <Debit>DebitID</Debit>
    <Tax>TaxID</Tax>
</Result>
aled
  • 21,330
  • 3
  • 27
  • 34
Sami
  • 9
  • 2

3 Answers3

1

Are trying to attempt something on these lines?

Script

%dw 2.0
output application/xml
---
Result: payload.*File map {
    Block: payload.File.Block,
    ($.*Identification map {
            (if($.PtID == "Z001") "BPay" else if ($.PtID == "Z002") "Payment" else if ($.PtID == "Z003") "Credit" else if ($.PtID == "Z004") "Debit" else  "Tax" ): $.BID
    }
    )
    
}

Output

<?xml version='1.0' encoding='UTF-8'?>
<Result>
  <Block>BlockID</Block>
  <BPay>BPayID</BPay>
  <Payment>PaymentID</Payment>
  <Credit>CreditID</Credit>
  <Debit>DebitID</Debit>
  <Tax>TaxID</Tax>
</Result>
Salim Khan
  • 4,233
  • 11
  • 15
1

Similar to other answers, with a different approach by keeping the column mapping separate from the code (this allows the mapping to be completely externalized if desired)

%dw 2.0
output application/XML
var columnMapping = {
    Z001: "Bpay",
    Z002: "Payment",
    Z003: "Credit",
    Z004: "Debit",
    Z005: "Tax"
}
---
Result: {
    Block: payload.File.Block,
    (payload.File.*Identification map (id) -> {
        (columnMapping[id.PtID] default "NULL"): id.BID
    } filterObject ($$ as String) != "NULL")
} 
sudhish_s
  • 573
  • 2
  • 5
0

I modified @SalimKhan already valid answer to use pattern matching (match...case) instead of if...else as a matter of personal preference.

%dw 2.0
output application/xml  
---
Result: payload.*File map {
    Block: payload.File.Block,
    ($.*Identification map {
      ($.PtID match {
        case is "Z001" -> "BPay"
        case is "Z002" -> "Payment"
        case is "Z003" -> "Credit"
        case is "Z004" -> "Debit"
        case is "Z005" -> "Tax"
        else -> dw::Runtime::fail("Unexpected PtID: $($)")
      }): $.BID
    })
}
aled
  • 21,330
  • 3
  • 27
  • 34