对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

照顾名称空间(必须正确定义它们)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...