I have requirement as print index of xml attribute
<Item>
<HeaderCharges>
<HeaderCharge ChargeCategory="ShippingCharge" ChargeName="ShippingCharge1" ChargeAmount="10.0" Reference="Free delivery">
<Extn BaseChargeAmount="10.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"/>
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingDiscount" ChargeName="ShippingDiscount1" ChargeAmount="6.0" Reference="Discount">
<Extn BaseChargeAmount="6.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"
POSDepartmentID="1097" PromotionID="100819" Description="" ReasonClass="PROMOVCH" ReasonCode="1230" ReasonDescription="50%off" />
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingDiscount" ChargeName="ShippingDiscount2" ChargeAmount="4.0" Reference="Discount 2">
<Extn BaseChargeAmount="4.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"
POSDepartmentID="1097" PromotionID="100819" ReasonClass="PROMOVCH" ReasonCode="1230" ReasonDescription="50%off" />
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingCharge" ChargeName="ShippingCharge1" ChargeAmount="10.0" Reference="Free delivery">
<Extn BaseChargeAmount="10.0" DeliveryCode="" DeliverySequenceNo="2" ConsignmentNo="0000164182" ItemID="1452778282"/>
</HeaderCharge>
</HeaderCharges>
</Item>
when ChargeCategory="ShippingCharge"
then index should be 1, for the next iteration index should be 2. Need to find index for ChargeCategory="ShippingCharge"
.
I tried with $$ but its supporting only for xml nodes, not for attributes
Code - `
%dw 1.0
%input payload application/xml
%output application/xml
%var counter=sizeOf payload.Order.OrderLines.*OrderLine
%var counter1=sizeOf payload.Order.HeaderCharges.*HeaderCharge
%var head=sizeOf payload.Order.HeaderCharges.*HeaderCharge.@ChargeCategory == "ShippingCharge" distinctBy $.@ChargeCategory
---
using (w=payload.Order.OrderLines.*OrderLine default [], a=payload.Order.PaymentMethods.*PaymentMethod default [],h=payload.Order.HeaderCharges.*HeaderCharge default [], z=payload.Order.HeaderCharges.*HeaderCharge filter $.ChargeCategory == "ShippingCharge" default [])
Transaction @(CancelFlag:"false"):{((payload.*Order default []) map {
RetailTransaction: {
((h default []) map ({ // HeaderCharge Level
LineItem @(EntryMethod:"Keyed"):{
SequenceNumber:counter + $$ + 1,
BeginDateTime:payload.Order.@OrderDate[0..18],
EndDateTime:payload.Order.PosLogDataList.PosLogData.@EndDateTime[0..18],
CustomerOrderForDelivery @(OrderStatus:"Sale"):{
Quantity:"1",
Associate:{
AssociateID:payload.Order.PosLogDataList.PosLogData.@AssociateID
},
(($ default []) groupBy $.@DeliverySequenceNo map ({
RetailPriceModifier @(MethodCode:"AutomaticPromotion",VoidFlag:"false"):{
SequenceNumber1: $$,
Amount @(Action:"Subtract"):$.Extn.@BaseChargePerUnit,
PreviousPrice:$.Extn.@BaseUnitPrice when $$ == 0 otherwise $.Extn.@BaseUnitPrice - [$$ - 1 ].Extn.@BaseChargePerUnit,
NewPrice:""
}
}) when $.@ChargeCategory == "ShippingCharge" otherwise {})
}
}
}
) when $.HeaderCharges != '' otherwise {})
}
}
)
}
` Here SequenceNumber1 should come as 1,2 as per above mentioned HeaderChargers condition
Sample input -
<?xml version="1.0" encoding="UTF-8"?>
<Order AuthorizationExpirationDate="2016-06-22T14:43:05+00:00"
BillToID="8801533001732" CarrierAccountNo=" " CarrierServiceCode=" "
ChargeActualFreightFlag="N" Createts="2016-06-22T14:43:05+00:00"
TermsCode=" " TotalAdjustmentAmount="0.00" evaluate="yes" isHistory="N">
<Extn BaseOrderTotal="385.00" BaseOriginalOrderTotal="385.00"
CustomerID="63873" InstoreID="147258" IsFraudCancelled="N"
IsMarketingPref="N" OrderTotal="385.00"
OriginalTotalAmount="385.00" TerminalID="704292"/>
<OrderLines>
<OrderLine AllocationDate="2016-06-24T00:00:00+00:00"
CarrierAccountNo=" " CarrierServiceCode="NDD_COU"
CustomerLinePONo=" " CustomerPONo=" " DeliveryCode=" "
DepartmentCode=" " FreightTerms=" " HoldFlag="N"
HoldReasonCode=" " ImportLicenseNo=" " InvoicedQty="0.00"
ScacAndServiceKey="" ShipNode="1097" ShipTogetherNo=" "
SplitQty="0.00" Status="Created" StatusQuantity="30.00"
SubLineNo="1" isHistory="N">
<OrderStatuses>
<OrderStatus OrderHeaderKey="2016062214430220261511"
OrderLineKey="2016062214430420261512"
OrderLineScheduleKey="201606221437430520261529"
OrderReleaseStatusKey="201606221477430520261530"
PipelineKey="20160222070748372596" Status="1100"
StatusDate="2016-06-22T14:43:04+00:00"
StatusDescription="Created" StatusQty="30.00" TotalQuantity="30.00"/>
</OrderStatuses>
<OrderDates>
<OrderDate ActualDate="2016-06-24T00:00:00+00:00"
CommittedDate="2016-06-24T00:00:00+00:00"
DateTypeId="YCD_FIRST_PROMISED_DATE"
OrderHeaderKey="2016062214430220261511"
OrderLineKey="2016062214430420261512" OrderReleaseKey=" "/>
</OrderDates>
<Instructions NumberOfInstructions="0"/>
<Item AliasName="" AliasValue="" CostCurrency=" "
CountryOfOrigin=" " CustomerItem=" "
CustomerItemDesc=" " ECCNNo=" "
HarmonizedCode="6203423100" ISBN=" "
ItemDesc="product "101351803" with name "501 Stonewash Straight Jeans""
ItemID="101351803"
ItemShortDesc=" "501 Stonewash Straight Jeans""
ItemWeight="0.70" ItemWeightUOM="KG"
ManufacturerItem=" " ManufacturerItemDesc=" "
ManufacturerName="Levi's" NMFCClass=" " NMFCCode=" "
NMFCDescription=" " ProductClass="Good" ProductLine=" "
ScheduleBCode=" " SupplierItem=" " SupplierItemDesc=" "
TaxProductCode="default" UPCCode=" " UnitCost="0.00" UnitOfMeasure="EACH"/>
<ItemDetails CanUseAsServiceTool="N" GlobalItemID=""
IsItemSuperseded="N" ItemGroupCode="PROD"
ItemID="101351803" ItemKey="20160406111557582111"
OrganizationCode="HOF" UnitOfMeasure="EACH">
<ItemAliasList>
<ItemAlias AliasName="EAN" AliasValue="5412456008434"/>
</ItemAliasList>
</ItemDetails>
<Extn AgeRestricted="N" BaseLineTotal="600.00"
BaseListPrice="12.00" BaseUnitPrice="20.00"
ConsignmentNo="0000217069-1" DeliverySequenceNo="1"
HybrisLineNo="1" LineTotal="600.00" POSDepartmentID="0049"/>
<LinePriceInfo DiscountPercentage="0.00" IsPriceLocked="N"
LineTotal="360.00" ListPrice="12.00" RetailPrice="0.00"
TaxableFlag="N" UnitPrice="20.00"/>
<PersonInfoShipTo AddressID="8802157101079"
AddressLine1="HighSroad1" AddressLine2="Nattf"
AddressLine3="" AddressLine4="" AddressLine5=""
AddressLine6="" AlternateEmailID="" Beeper=""
City="Trava1234" Company="" Country="UK" DayFaxNo=""
DayPhone="" Department="" EMailID="" EveningFaxNo=""
EveningPhone="" FirstName="Ferand" JobTitle=""
LastName="abcd" MiddleName="" MobilePhone="9876551111"
OtherPhone="" PersonID=""
PersonInfoKey="2016062214430520261527" State=""
Suffix="" Title="Mr" ZipCode="2000"/>
<LineCharges>
<LineCharge ChargeAmount="240.00"
ChargeCategory="SystemPromotion"
ChargeName="12% on purchase over paypal"
ChargeNameKey="" ChargePerLine="0.00"
ChargePerUnit="8.00" InvoicedChargeAmount="0.00"
InvoicedChargePerLine="0.00"
InvoicedChargePerUnit="0.00" IsBillable="Y"
IsDiscount="Y"
Reference="percentage off on purchase over paypal"
RemainingChargeAmount="240.00"
RemainingChargePerLine="0.00" RemainingChargePerUnit="8.00">
<Extn BaseChargeAmount="240.00"
BaseChargePerLine="0.00"
BaseChargePerUnit="8.00"
PromotionID="0000100253"
ReasonClass="12% on purchase over debit card"
ReasonCode="12% on purchase over debit card" ReasonDescription="percentage off on purchase over paypal"/>
</LineCharge>
</LineCharges>
</OrderLine>
</OrderLines>
<HeaderCharges>
<HeaderCharge ChargeCategory="ShippingCharge" ChargeName="ShippingCharge1" ChargeAmount="10.0" Reference="Free delivery">
<Extn BaseChargeAmount="10.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"/>
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingDiscount" ChargeName="ShippingDiscount1" ChargeAmount="6.0" Reference="Discount">
<Extn BaseChargeAmount="6.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"
POSDepartmentID="1097" PromotionID="100819" Description="" ReasonClass="PROMOVCH" ReasonCode="1230" ReasonDescription="50%off" />
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingDiscount" ChargeName="ShippingDiscount2" ChargeAmount="4.0" Reference="Discount 2">
<Extn BaseChargeAmount="4.0" DeliveryCode="" DeliverySequenceNo="1" ConsignmentNo="0000164182" ItemID="1452778282"
POSDepartmentID="1097" PromotionID="100819" ReasonClass="PROMOVCH" ReasonCode="1230" ReasonDescription="50%off" />
</HeaderCharge>
<HeaderCharge ChargeCategory="ShippingCharge" ChargeName="ShippingCharge1" ChargeAmount="10.0" Reference="Free delivery">
<Extn BaseChargeAmount="10.0" DeliveryCode="" DeliverySequenceNo="2" ConsignmentNo="0000164182" ItemID="1452778282"/>
</HeaderCharge>
</HeaderCharges>
</Order>