Summing the values of two specific nodes in xpath 1.0

165 views Asked by At

I am trying to sum two specific values in xpath 1.0. Currently I use specific xpath to pull the field I want, because I then map it to a JSON output.

Here is a sample of the xml:

<ns2:Pricing>
                  <ns2:Price>
                    <ns2:ChargeAmount>52247.00</ns2:ChargeAmount>
                    <ns2:PriceDescription>act_237</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>558.40</ns2:ChargeAmount>
                    <ns2:PriceDescription>adv_261</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>45920.80</ns2:ChargeAmount>
                    <ns2:PriceDescription>base_model_invoice_price</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>48800.00</ns2:ChargeAmount>
                    <ns2:PriceDescription>base_model_msrp</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>1675.20</ns2:ChargeAmount>
                    <ns2:PriceDescription>dealer_holdback</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>558.40</ns2:ChargeAmount>
                    <ns2:PriceDescription>dealer_imr_contribution</ns2:PriceDescription>
                  </ns2:Price>
                  <ns2:Price>
                    <ns2:ChargeAmount>3141.64</ns2:ChargeAmount>
                    <ns2:PriceDescription>employee_incentive_amount</ns2:PriceDescription>
                  </ns2:Price>

And the xpath I am using to pull some of these values is:

/SOAP-ENV:Envelope/SOAP-ENV:Body/ns4:ProcessMessageResponse/ns4:payload/ns4:content/ns2:ShowVehicleInvoice/ns2:ShowVehicleInvoiceDataArea/ns2:VehicleInvoice/ns2:Invoice/ns2:VehicleInvoiceVehicleLineItem/ns2:Pricing/ns2:Price/../ns2:Price[ns2:PriceDescription='act_237']/ns2:ChargeAmount

and

/SOAP-ENV:Envelope/SOAP-ENV:Body/ns4:ProcessMessageResponse/ns4:payload/ns4:content/ns2:ShowVehicleInvoice/ns2:ShowVehicleInvoiceDataArea/ns2:VehicleInvoice/ns2:Invoice/ns2:VehicleInvoiceVehicleLineItem/ns2:Pricing/ns2:Price/../ns2:Price[ns2:PriceDescription='dealer_holdback']/ns2:ChargeAmount

How can I in xpath 1.0 sum these two fields?

1

There are 1 answers

1
zx485 On BEST ANSWER

You can use this XPath-1.0 expression to get the sum:

/SOAP-ENV:Envelope/SOAP-ENV:Body/ns4:ProcessMessageResponse/ns4:payload/ns4:content/ns2:ShowVehicleInvoice/ns2:ShowVehicleInvoiceDataArea/ns2:VehicleInvoice/ns2:Invoice/ns2:VehicleInvoiceVehicleLineItem/ns2:Pricing/ns2:Price[ns2:PriceDescription='act_237']/ns2:ChargeAmount + /SOAP-ENV:Envelope/SOAP-ENV:Body/ns4:ProcessMessageResponse/ns4:payload/ns4:content/ns2:ShowVehicleInvoice/ns2:ShowVehicleInvoiceDataArea/ns2:VehicleInvoice/ns2:Invoice/ns2:VehicleInvoiceVehicleLineItem/ns2:Pricing/ns2:Price[ns2:PriceDescription='dealer_holdback']/ns2:ChargeAmount

Take care of the namespaces (they must all be correctly defined).