求和 xpath 1.0 中两个特定节点的值

Summing the values of two specific nodes in xpath 1.0

我正在尝试对 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

注意命名空间(必须正确定义它们)。