Find index of xml attributes using mule dataweave -
i have requirement 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"
index should 1, next iteration index should 2. need find index chargecategory="shippingcharge"
.
i tried $$ supporting xml nodes, not 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 1,2 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" 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 on 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 on 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 on debit card" reasoncode="12% on purchase on debit card" reasondescription="percentage off on purchase on 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>
i not sure of functionality, think of alternative.instead of looping through headercharge elements loop through them satisfies condition , again loop through them doesnt satisfy. way when $$ can separate sequence number objects with'shippingcharge'
eg:
((h[?($.@chargecategory != "shippingcharge")] default []) map ({ //ur code here when not equals shipping charges } ) when $.headercharges != '' otherwise {}), ((h[?($.@chargecategory == "shippingcharge")] default []) map ({ //ur code here when equals shipping charges } ) when $.headercharges != '' otherwise {})
Comments
Post a Comment