xml - How to write Xpath Query in Groovy Code SoapUI? -


i new groovy scripting , trying write code should give output xpath:

/billinginvoice/invoicedorders/entry/value/lines/aggregationkey[text()='inv_group_meterreading' or text()='inv_group_power']/../(aggregationkey|amount) 

this xpath works fine in online xpath formatting check, couldn't make work in groovy script don't have knowledge of it.

the purpose of writing groovy script here extract amounts corresponding aggregation keys , sum, stuck @ getting tag elements , values.

below xml using, saved in soapui property. parsing in groovy script:

<billinginvoice>    <aggregation>       <aggregations>          <aggregations>             <key>inv_group_power</key>             <showunitprice>true</showunitprice>             <showvolume>true</showvolume>             <volumeuom>kw</volumeuom>          </aggregations>          <aggregations>             <key>inv_group_meterreading</key>             <showunitprice>true</showunitprice>             <showvolume>true</showvolume>             <volumeuom>days</volumeuom>          </aggregations>          <key>distribution</key>          <showunitprice>false</showunitprice>          <showvolume>false</showvolume>       </aggregations>    </aggregation>    <amount>153.6600000000</amount>    <balance>153.6600000000</balance>    <bundleid>000000~default</bundleid>    <consumptionevolution>       <consumption>          <deliverypointid>555555555555555</deliverypointid>          <endperiod>2017-07-31t00:00:00+02:00</endperiod>          <startperiod>2016-08-01t00:00:00+02:00</startperiod>          <timeframe>total_hour</timeframe>          <value>74645.2300000000</value>          <meteringcontext>periodic_metering</meteringcontext>       </consumption>       <consumption>          <deliverypointid>555555555555555</deliverypointid>          <endperiod>2015-05-07t00:00:00+02:00</endperiod>          <startperiod>2015-03-19t00:00:00+01:00</startperiod>          <timeframe>total_hour</timeframe>          <value>471.8600000000</value>          <meteringcontext>gain</meteringcontext>       </consumption>       <consumption>          <deliverypointid>555555555555555</deliverypointid>          <endperiod>2015-03-19t00:00:00+01:00</endperiod>          <startperiod>2014-11-01t00:00:00+01:00</startperiod>          <timeframe>total_hour</timeframe>          <value>3860.2600000000</value>          <meteringcontext>gain</meteringcontext>       </consumption>    </consumptionevolution>    <customer>       <accountnr>999999999</accountnr>       <billingid>444444444</billingid>    </customer>    <duedate>2017-08-21t00:00:00+02:00</duedate>    <dunningblockedbyagent>false</dunningblockedbyagent>    <dunningblockedbysystem>false</dunningblockedbysystem>    <dunningstatus>nid</dunningstatus>    <eantotalevolution>       <totalevolution>          <connectionkey>555555555555555</connectionkey>          <eantotalconsumption/>       </totalevolution>    </eantotalevolution>    <invoicedate>2017-08-07t00:00:00+02:00</invoicedate>    <invoicenumber>ghjkj111111</invoicenumber>    <invoicetype>settlement</invoicetype>    <invoicedorders>       <entry>          <key>59412</key>          <value>             <address>                <city>stackoverflow</city>                <country>us</country>                <deliverypointid>555555555555555</deliverypointid>                <gridcompany>4444444444444</gridcompany>                <region>cold</region>                <streetname>placeforeveryone</streetname>                <streetnumber>420</streetnumber>                <zipcode>xxxxxx</zipcode>             </address>             <billingestimates>                <billingestimate>                   <fromdate>2015-11-01t00:00:00+01:00</fromdate>                   <logdate>2017-08-07t00:00:00+02:00</logdate>                   <meteringcontext>gain</meteringcontext>                   <source>market</source>                   <timeframe>total_hour</timeframe>                   <validfrom>2015-11-01t00:00:00+01:00</validfrom>                   <validto>2015-11-01t00:00:00+01:00</validto>                   <value>46802.0000000000</value>                </billingestimate>                <billingestimate>                   <fromdate>2015-11-01t00:00:00+01:00</fromdate>                   <logdate>2017-08-07t00:00:00+02:00</logdate>                   <meteringcontext>gain</meteringcontext>                   <source>market</source>                   <timeframe>total_hour</timeframe>                   <validfrom>2015-11-01t00:00:00+01:00</validfrom>                   <validto>9999-12-31t00:00:00+01:00</validto>                   <value>12409.0000000000</value>                </billingestimate>             </billingestimates>             <contractline>00000-0000-0000-57af-00000</contractline>             <contractnumber>00000-00000-00000-8cbe-00000</contractnumber>             <contractedvolumes>                <fromdate>2015-11-01t00:00:00+01:00</fromdate>                <timeframe>total_hour</timeframe>                <todate>9999-12-31t00:00:00+01:00</todate>                <value>200000.0000000000</value>             </contractedvolumes>             <historicalconsumptions>                <deliverypointid>555555555555555</deliverypointid>                <endperiod>2014-11-01t00:00:00+01:00</endperiod>                <startperiod>2014-04-11t00:00:00+02:00</startperiod>                <timeframe>total_hour</timeframe>                <value>1057.6900000000</value>             </historicalconsumptions>             <historicalconsumptions>                <deliverypointid>555555555555555</deliverypointid>                <endperiod>2015-03-19t00:00:00+01:00</endperiod>                <startperiod>2014-11-01t00:00:00+01:00</startperiod>                <timeframe>total_hour</timeframe>                <value>3860.2600000000</value>             </historicalconsumptions>             <historicalconsumptions>                <deliverypointid>555555555555555</deliverypointid>                <endperiod>2015-05-07t00:00:00+02:00</endperiod>                <startperiod>2015-03-19t00:00:00+01:00</startperiod>                <timeframe>total_hour</timeframe>                <value>471.8600000000</value>             </historicalconsumptions>             <lines>                <aggregationkey>inv_group_vat_21%</aggregationkey>                <amount>8.2400000000</amount>                <billingtype>settlement</billingtype>                <description>vat 21% 2017-01-01/2017-07-31</description>                <invoicelineid>200196</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-07-31t00:00:00+02:00</periodend>                <periodstart>2017-01-01t00:00:00+01:00</periodstart>                <price>21.0000000000</price>                <productcode>vat_21%</productcode>                <quantity>39.2500000000</quantity>                <vatbaseamount>39.2500000000</vatbaseamount>                <vatcode>vat-out-21-s</vatcode>                <vatperc>21.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_vat_0%</aggregationkey>                <amount>0.0000000000</amount>                <billingtype>settlement</billingtype>                <description>vat 0%  2017-01-01/2017-07-31</description>                <invoicelineid>200195</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-07-31t00:00:00+02:00</periodend>                <periodstart>2017-01-01t00:00:00+01:00</periodstart>                <price>0.0000000000</price>                <productcode>vat_0%</productcode>                <quantity>0.0000000000</quantity>                <vatbaseamount>0.0000000000</vatbaseamount>                <vatcode>vat-out-00-s</vatcode>                <vatperc>0.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_meterreading</aggregationkey>                <amount>2.8000000000</amount>                <billingtype>settlement</billingtype>                <description>d gas meterreading 2017-01-01/2017-07-31</description>                <invoicelineid>200194</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-07-31t00:00:00+02:00</periodend>                <periodstart>2017-01-01t00:00:00+01:00</periodstart>                <price>0.0132876700</price>                <productcode>d_gas_meterreading</productcode>                <quantity>211.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_power</aggregationkey>                <amount>36.4500000000</amount>                <billingtype>settlement</billingtype>                <description>d gas fixed term 2017-01-01/2017-07-31</description>                <invoicelineid>200193</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-07-31t00:00:00+02:00</periodend>                <periodstart>2017-01-01t00:00:00+01:00</periodstart>                <price>0.1727671200</price>                <productcode>d_gas_fixed_term</productcode>                <quantity>211.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_vat_0%</aggregationkey>                <amount>0.0000000000</amount>                <billingtype>settlement</billingtype>                <description>vat 0%  2016-03-01/2017-01-01</description>                <invoicelineid>200192</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-01-01t00:00:00+01:00</periodend>                <periodstart>2016-03-01t00:00:00+01:00</periodstart>                <price>0.0000000000</price>                <productcode>vat_0%</productcode>                <quantity>0.0000000000</quantity>                <vatbaseamount>0.0000000000</vatbaseamount>                <vatcode>vat-out-00-s</vatcode>                <vatperc>0.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_vat_21%</aggregationkey>                <amount>13.0300000000</amount>                <billingtype>settlement</billingtype>                <description>vat 21% 2016-03-01/2017-01-01</description>                <invoicelineid>200191</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-01-01t00:00:00+01:00</periodend>                <periodstart>2016-03-01t00:00:00+01:00</periodstart>                <price>21.0000000000</price>                <productcode>vat_21%</productcode>                <quantity>62.0500000000</quantity>                <vatbaseamount>62.0500000000</vatbaseamount>                <vatcode>vat-out-21-s</vatcode>                <vatperc>21.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_meterreading</aggregationkey>                <amount>6.7900000000</amount>                <billingtype>settlement</billingtype>                <description>d gas meterreading 2016-03-01/2017-01-01</description>                <invoicelineid>200190</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-01-01t00:00:00+01:00</periodend>                <periodstart>2016-03-01t00:00:00+01:00</periodstart>                <price>0.0221857900</price>                <productcode>d_gas_meterreading</productcode>                <quantity>306.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_power</aggregationkey>                <amount>55.2600000000</amount>                <billingtype>settlement</billingtype>                <description>d gas fixed term 2016-03-01/2017-01-01</description>                <invoicelineid>200189</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2017-01-01t00:00:00+01:00</periodend>                <periodstart>2016-03-01t00:00:00+01:00</periodstart>                <price>0.1805737700</price>                <productcode>d_gas_fixed_term</productcode>                <quantity>306.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_vat_21%</aggregationkey>                <amount>1.2300000000</amount>                <billingtype>settlement</billingtype>                <description>vat 21% 2016-02-01/2016-03-01</description>                <invoicelineid>200188</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-03-01t00:00:00+01:00</periodend>                <periodstart>2016-02-01t00:00:00+01:00</periodstart>                <price>21.0000000000</price>                <productcode>vat_21%</productcode>                <quantity>5.8800000000</quantity>                <vatbaseamount>5.8800000000</vatbaseamount>                <vatcode>vat-out-21-s</vatcode>                <vatperc>21.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_vat_0%</aggregationkey>                <amount>0.0000000000</amount>                <billingtype>settlement</billingtype>                <description>vat 0%  2016-02-01/2016-03-01</description>                <invoicelineid>200187</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-03-01t00:00:00+01:00</periodend>                <periodstart>2016-02-01t00:00:00+01:00</periodstart>                <price>0.0000000000</price>                <productcode>vat_0%</productcode>                <quantity>0.0000000000</quantity>                <vatbaseamount>0.0000000000</vatbaseamount>                <vatcode>vat-out-00-s</vatcode>                <vatperc>0.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_meterreading</aggregationkey>                <amount>0.6400000000</amount>                <billingtype>settlement</billingtype>                <description>d gas meterreading 2016-02-01/2016-03-01</description>                <invoicelineid>200186</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-03-01t00:00:00+01:00</periodend>                <periodstart>2016-02-01t00:00:00+01:00</periodstart>                <price>0.0221857900</price>                <productcode>d_gas_meterreading</productcode>                <quantity>29.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_power</aggregationkey>                <amount>5.2400000000</amount>                <billingtype>settlement</billingtype>                <description>d gas fixed term 2016-02-01/2016-03-01</description>                <invoicelineid>200185</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-03-01t00:00:00+01:00</periodend>                <periodstart>2016-02-01t00:00:00+01:00</periodstart>                <price>0.1805737700</price>                <productcode>d_gas_fixed_term</productcode>                <quantity>29.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_vat_21%</aggregationkey>                <amount>1.3200000000</amount>                <billingtype>settlement</billingtype>                <description>vat 21% 2016-01-01/2016-02-01</description>                <invoicelineid>200184</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-02-01t00:00:00+01:00</periodend>                <periodstart>2016-01-01t00:00:00+01:00</periodstart>                <price>21.0000000000</price>                <productcode>vat_21%</productcode>                <quantity>6.2900000000</quantity>                <vatbaseamount>6.2900000000</vatbaseamount>                <vatcode>vat-out-21-s</vatcode>                <vatperc>21.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_vat_0%</aggregationkey>                <amount>0.0000000000</amount>                <billingtype>settlement</billingtype>                <description>vat 0%  2016-01-01/2016-02-01</description>                <invoicelineid>200183</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-02-01t00:00:00+01:00</periodend>                <periodstart>2016-01-01t00:00:00+01:00</periodstart>                <price>0.0000000000</price>                <productcode>vat_0%</productcode>                <quantity>0.0000000000</quantity>                <vatbaseamount>0.0000000000</vatbaseamount>                <vatcode>vat-out-00-s</vatcode>                <vatperc>0.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_meterreading</aggregationkey>                <amount>0.6900000000</amount>                <billingtype>settlement</billingtype>                <description>d gas meterreading 2016-01-01/2016-02-01</description>                <invoicelineid>200182</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-02-01t00:00:00+01:00</periodend>                <periodstart>2016-01-01t00:00:00+01:00</periodstart>                <price>0.0221857900</price>                <productcode>d_gas_meterreading</productcode>                <quantity>31.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_power</aggregationkey>                <amount>5.6000000000</amount>                <billingtype>settlement</billingtype>                <description>d gas fixed term 2016-01-01/2016-02-01</description>                <invoicelineid>200181</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-02-01t00:00:00+01:00</periodend>                <periodstart>2016-01-01t00:00:00+01:00</periodstart>                <price>0.1805737700</price>                <productcode>d_gas_fixed_term</productcode>                <quantity>31.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_vat_0%</aggregationkey>                <amount>0.0000000000</amount>                <billingtype>settlement</billingtype>                <description>vat 0%  2015-11-01/2016-01-01</description>                <invoicelineid>200180</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-01-01t00:00:00+01:00</periodend>                <periodstart>2015-11-01t00:00:00+01:00</periodstart>                <price>0.0000000000</price>                <productcode>vat_0%</productcode>                <quantity>0.0000000000</quantity>                <vatbaseamount>0.0000000000</vatbaseamount>                <vatcode>vat-out-00-s</vatcode>                <vatperc>0.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_vat_21%</aggregationkey>                <amount>2.8400000000</amount>                <billingtype>settlement</billingtype>                <description>vat 21% 2015-11-01/2016-01-01</description>                <invoicelineid>200179</invoicelineid>                <ispercentage>1</ispercentage>                <isvatpercentage>true</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-01-01t00:00:00+01:00</periodend>                <periodstart>2015-11-01t00:00:00+01:00</periodstart>                <price>21.0000000000</price>                <productcode>vat_21%</productcode>                <quantity>13.5300000000</quantity>                <vatbaseamount>13.5300000000</vatbaseamount>                <vatcode>vat-out-21-s</vatcode>                <vatperc>21.0000000000</vatperc>             </lines>             <lines>                <aggregationkey>inv_group_meterreading</aggregationkey>                <amount>1.6600000000</amount>                <billingtype>settlement</billingtype>                <description>d gas meterreading 2015-11-01/2016-01-01</description>                <invoicelineid>200178</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-01-01t00:00:00+01:00</periodend>                <periodstart>2015-11-01t00:00:00+01:00</periodstart>                <price>0.0272602700</price>                <productcode>d_gas_meterreading</productcode>                <quantity>61.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <lines>                <aggregationkey>inv_group_power</aggregationkey>                <amount>11.8700000000</amount>                <billingtype>settlement</billingtype>                <description>d gas fixed term 2015-11-01/2016-01-01</description>                <invoicelineid>200177</invoicelineid>                <ispercentage>0</ispercentage>                <isvatpercentage>false</isvatpercentage>                <orderlinetype>settlement</orderlinetype>                <periodend>2016-01-01t00:00:00+01:00</periodend>                <periodstart>2015-11-01t00:00:00+01:00</periodstart>                <price>0.1946575300</price>                <productcode>d_gas_fixed_term</productcode>                <quantity>61.0000000000</quantity>                <vatcode>vat-out-21-s</vatcode>             </lines>             <metertype>year</metertype>             <newadvanced>                <recurringamount>42.0000000000</recurringamount>                <recurringunit>month</recurringunit>                <recurringvalue>1</recurringvalue>             </newadvanced>             <ordertype>settlement</ordertype>             <periodend>2017-07-31t00:00:00+02:00</periodend>             <periodstart>2015-11-01t00:00:00+01:00</periodstart>             <producttype>ng</producttype>             <subamount>153.6600000000</subamount>          </value>       </entry>    </invoicedorders>    <paymentreference>+++000/000000/00000000+++</paymentreference>    <relationref>88888888</relationref>    <status>2</status>    <unsettledinvoices/>    <withattachment>false</withattachment>    <yearlyconsumptionevolution>       <yearlyconsumptionperean>          <connectionkey>555555555555555</connectionkey>          <yearlyconsumptions>             <yearlyconsumption>                <timeframeconsumptions>                   <timeframeconsumption>                      <consumption>44639.0266771523</consumption>                      <timeframe>total_hour</timeframe>                   </timeframeconsumption>                </timeframeconsumptions>                <year>2017</year>             </yearlyconsumption>             <yearlyconsumption>                <timeframeconsumptions>                   <timeframeconsumption>                      <consumption>5257.2047645253</consumption>                      <timeframe>total_hour</timeframe>                   </timeframeconsumption>                </timeframeconsumptions>                <year>2015</year>             </yearlyconsumption>             <yearlyconsumption>                <timeframeconsumptions>                   <timeframeconsumption>                      <consumption>4971.3241391945</consumption>                      <timeframe>total_hour</timeframe>                   </timeframeconsumption>                </timeframeconsumptions>                <year>2014</year>             </yearlyconsumption>          </yearlyconsumptions>       </yearlyconsumptionperean>    </yearlyconsumptionevolution> </billinginvoice> 

here groovy script looking for.

approach #1 using xmlslurper

i.e., searches aggregationkey 1 of possible values , find values of respective amount , sum them.

//define key & possible values filter def filtermap = [aggregationkey:['inv_group_meterreading','inv_group_power']]  //pass xmlstring below, not added saving space def xml = new xmlslurper().parsetext(xmlstring)  def result = filtermap.collect{ k,v -> xml.'**'.findall{it.name() == k && in v}*.parent()*.amount*.text()}.flatten().sum{it double} log.info result 

you can try online demo

approach #2 using xpath

note right xpath retrieving required amount , sum sum(//lines[aggregationkey[.='inv_group_meterreading' or .='inv_group_power']]/amount)

of course, if there namespaces needs handled well.

script

import com.eviware.soapui.support.xmlholder //pass xmlstring, avoiding save space def holder = new xmlholder(xmlstring) def result = holder.getnodevalue("sum(//*:lines[aggregationkey[.='inv_group_meterreading' or .='inv_group_power']]/*:amount)") double log.info result 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -