问题描述
我试图在xpath 1.0中求和两个特定值。目前,我使用特定的xpath提取所需的字段,因为然后将其映射到JSON输出。
这是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>
我用来提取其中一些值的xpath是:
/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
和
/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
如何在xpath 1.0中对这两个字段求和?
解决方法
您可以使用此XPath-1.0表达式获取总和:
/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
照顾名称空间(必须正确定义它们)。