1

我有来自 UPS Rates API 的以下 XML 响应,我需要仅在 RateType 节点 = 'RATED_ACCOUNT_SHIPMENT' 的情况下为每个 'RateReplyDetails' 获取 'ServiceType'、'Amount' 和 'Currency' 节点的值。

我的尝试如下,ServiceType 有效,但金额和货币无效。有人可以帮忙吗?

$RateReply = $fedex_xml->children("env", true)->Body->children("v13", true)->RateReply;

 foreach($RateReply->RateReplyDetails as $details){
    $service = $details->ServiceType;
    $amount = $details->xpath("/RatedShipmentDetails/ShipmentRateDetail/RateType['RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Amount");
    $currency = $details->xpath("/RatedShipmentDetails/ShipmentRateDetail/RateType['RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Currency");

 }

<soapenv:Envelope><env:Header/><env:Body><v13:RateReply><v13:HighestSeverity>SUCCESS</v13:HighestSeverity><v13:Notifications><v13:Severity>SUCCESS</v13:Severity><v13:Source>crs</v13:Source><v13:Code>0</v13:Code><v13:Message>Request was successfully processed. </v13:Message><v13:LocalizedMessage>Request was successfully processed. </v13:LocalizedMessage></v13:Notifications><ns1:TransactionDetail><ns1:CustomerTransactionId> *** Rate Available Services Request v13 using PHP ***</ns1:CustomerTransactionId></ns1:TransactionDetail><ns1:Version><ns1:ServiceId>crs</ns1:ServiceId><ns1:Major>13</ns1:Major><ns1:Intermediate>0</ns1:Intermediate><ns1:Minor>0</ns1:Minor></ns1:Version><v13:RateReplyDetails><v13:ServiceType>INTERNATIONAL_FIRST</v13:ServiceType><v13:PackagingType>YOUR_PACKAGING</v13:PackagingType><v13:DeliveryStation>DKKA </v13:DeliveryStation><v13:DeliveryDayOfWeek>THU</v13:DeliveryDayOfWeek><v13:DeliveryTimestamp>2012-12-13T09:00:00</v13:DeliveryTimestamp><v13:CommitDetails><v13:CommodityName>DOCUMENTS</v13:CommodityName><v13:ServiceType>INTERNATIONAL_FIRST</v13:ServiceType><v13:CommitTimestamp>2012-12-13T09:00:00</v13:CommitTimestamp><v13:DayOfWeek>THU</v13:DayOfWeek><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:BrokerToDestinationDays>0</v13:BrokerToDestinationDays><v13:CommitMessages><v13:Code>134</v13:Code><v13:Message>REQUEST COMPLETED</v13:Message></v13:CommitMessages><v13:DeliveryMessages> 9:00 A.M. IF NO CUSTOMS DELAY</v13:DeliveryMessages><v13:DocumentContent>DOCUMENTS_ONLY</v13:DocumentContent><v13:RequiredDocuments>INTERNATIONAL_AIRWAY_BILL</v13:RequiredDocuments></v13:CommitDetails><v13:DestinationAirportId>BUF</v13:DestinationAirportId><v13:IneligibleForMoneyBackGuarantee>false</v13:IneligibleForMoneyBackGuarantee><v13:OriginServiceArea>AM</v13:OriginServiceArea><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:SignatureOption>INDIRECT</v13:SignatureOption><v13:ActualRateType>PAYOR_ACCOUNT_SHIPMENT</v13:ActualRateType><v13:RatedShipmentDetails><v13:ShipmentRateDetail><v13:RateType>PAYOR_ACCOUNT_SHIPMENT</v13:RateType><v13:RateScale>0000000</v13:RateScale><v13:RateZone>GB001O</v13:RateZone><v13:PricingCode>ACTUAL</v13:PricingCode><v13:RatedWeightMethod>ACTUAL</v13:RatedWeightMethod><v13:CurrencyExchangeRate><v13:FromCurrency>UKL</v13:FromCurrency><v13:IntoCurrency>USD</v13:IntoCurrency><v13:Rate>1.59</v13:Rate></v13:CurrencyExchangeRate><v13:DimDivisor>0</v13:DimDivisor><v13:FuelSurchargePercent>17.5</v13:FuelSurchargePercent><v13:TotalBillingWeight><v13:Units>KG</v13:Units><v13:Value>1.0</v13:Value></v13:TotalBillingWeight><v13:TotalBaseCharge><v13:Currency>USD</v13:Currency><v13:Amount>129.74</v13:Amount></v13:TotalBaseCharge><v13:TotalFreightDiscounts><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalFreightDiscounts><v13:TotalNetFreight><v13:Currency>USD</v13:Currency><v13:Amount>129.74</v13:Amount></v13:TotalNetFreight><v13:TotalSurcharges><v13:Currency>USD</v13:Currency><v13:Amount>22.71</v13:Amount></v13:TotalSurcharges><v13:TotalNetFedExCharge><v13:Currency>USD</v13:Currency><v13:Amount>152.45</v13:Amount></v13:TotalNetFedExCharge><v13:TotalTaxes><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalTaxes><v13:TotalNetCharge><v13:Currency>USD</v13:Currency><v13:Amount>152.45</v13:Amount></v13:TotalNetCharge><v13:TotalRebates><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalRebates><v13:Surcharges><v13:SurchargeType>FUEL</v13:SurchargeType><v13:Description>Fuel</v13:Description><v13:Amount><v13:Currency>USD</v13:Currency><v13:Amount>22.71</v13:Amount></v13:Amount></v13:Surcharges></v13:ShipmentRateDetail></v13:RatedShipmentDetails><v13:RatedShipmentDetails><v13:ShipmentRateDetail><v13:RateType>RATED_ACCOUNT_SHIPMENT</v13:RateType><v13:RateScale>0000000</v13:RateScale><v13:RateZone>GB001O</v13:RateZone><v13:PricingCode>ACTUAL</v13:PricingCode><v13:RatedWeightMethod>ACTUAL</v13:RatedWeightMethod><v13:CurrencyExchangeRate><v13:FromCurrency>UKL</v13:FromCurrency><v13:IntoCurrency>UKL</v13:IntoCurrency><v13:Rate>1.0</v13:Rate></v13:CurrencyExchangeRate><v13:DimDivisor>0</v13:DimDivisor><v13:FuelSurchargePercent>17.5</v13:FuelSurchargePercent><v13:TotalBillingWeight><v13:Units>KG</v13:Units><v13:Value>1.0</v13:Value></v13:TotalBillingWeight><v13:TotalBaseCharge><v13:Currency>UKL</v13:Currency><v13:Amount>81.7</v13:Amount></v13:TotalBaseCharge><v13:TotalFreightDiscounts><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalFreightDiscounts><v13:TotalNetFreight><v13:Currency>UKL</v13:Currency><v13:Amount>81.7</v13:Amount></v13:TotalNetFreight><v13:TotalSurcharges><v13:Currency>UKL</v13:Currency><v13:Amount>14.3</v13:Amount></v13:TotalSurcharges><v13:TotalNetFedExCharge><v13:Currency>UKL</v13:Currency><v13:Amount>96.0</v13:Amount></v13:TotalNetFedExCharge><v13:TotalTaxes><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalTaxes><v13:TotalNetCharge><v13:Currency>UKL</v13:Currency><v13:Amount>96.0</v13:Amount></v13:TotalNetCharge><v13:TotalRebates><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalRebates><v13:Surcharges><v13:SurchargeType>FUEL</v13:SurchargeType><v13:Description>Fuel</v13:Description><v13:Amount><v13:Currency>UKL</v13:Currency><v13:Amount>14.3</v13:Amount></v13:Amount></v13:Surcharges></v13:ShipmentRateDetail></v13:RatedShipmentDetails></v13:RateReplyDetails><v13:RateReplyDetails><v13:ServiceType>INTERNATIONAL_PRIORITY</v13:ServiceType><v13:PackagingType>YOUR_PACKAGING</v13:PackagingType><v13:DeliveryStation>DKKA </v13:DeliveryStation><v13:DeliveryDayOfWeek>THU</v13:DeliveryDayOfWeek><v13:DeliveryTimestamp>2012-12-13T10:30:00</v13:DeliveryTimestamp><v13:CommitDetails><v13:CommodityName>DOCUMENTS</v13:CommodityName><v13:ServiceType>INTERNATIONAL_PRIORITY</v13:ServiceType><v13:CommitTimestamp>2012-12-13T10:30:00</v13:CommitTimestamp><v13:DayOfWeek>THU</v13:DayOfWeek><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:BrokerToDestinationDays>0</v13:BrokerToDestinationDays><v13:CommitMessages><v13:Code>134</v13:Code><v13:Message>REQUEST COMPLETED</v13:Message></v13:CommitMessages><v13:DeliveryMessages>10:30 A.M. IF NO CUSTOMS DELAY</v13:DeliveryMessages><v13:DocumentContent>DOCUMENTS_ONLY</v13:DocumentContent><v13:RequiredDocuments>INTERNATIONAL_AIRWAY_BILL</v13:RequiredDocuments></v13:CommitDetails><v13:DestinationAirportId>BUF</v13:DestinationAirportId><v13:IneligibleForMoneyBackGuarantee>false</v13:IneligibleForMoneyBackGuarantee><v13:OriginServiceArea>AM</v13:OriginServiceArea><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:SignatureOption>INDIRECT</v13:SignatureOption><v13:ActualRateType>PAYOR_ACCOUNT_SHIPMENT</v13:ActualRateType><v13:RatedShipmentDetails><v13:ShipmentRateDetail><v13:RateType>PAYOR_ACCOUNT_SHIPMENT</v13:RateType><v13:RateScale>0000000</v13:RateScale><v13:RateZone>GB001O</v13:RateZone><v13:PricingCode>ACTUAL</v13:PricingCode><v13:RatedWeightMethod>ACTUAL</v13:RatedWeightMethod><v13:CurrencyExchangeRate><v13:FromCurrency>UKL</v13:FromCurrency><v13:IntoCurrency>USD</v13:IntoCurrency><v13:Rate>1.59</v13:Rate></v13:CurrencyExchangeRate><v13:DimDivisor>0</v13:DimDivisor><v13:FuelSurchargePercent>17.5</v13:FuelSurchargePercent><v13:TotalBillingWeight><v13:Units>KG</v13:Units><v13:Value>1.0</v13:Value></v13:TotalBillingWeight><v13:TotalBaseCharge><v13:Currency>USD</v13:Currency><v13:Amount>98.46</v13:Amount></v13:TotalBaseCharge><v13:TotalFreightDiscounts><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalFreightDiscounts><v13:TotalNetFreight><v13:Currency>USD</v13:Currency><v13:Amount>98.46</v13:Amount></v13:TotalNetFreight><v13:TotalSurcharges><v13:Currency>USD</v13:Currency><v13:Amount>17.23</v13:Amount></v13:TotalSurcharges><v13:TotalNetFedExCharge><v13:Currency>USD</v13:Currency><v13:Amount>115.69</v13:Amount></v13:TotalNetFedExCharge><v13:TotalTaxes><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalTaxes><v13:TotalNetCharge><v13:Currency>USD</v13:Currency><v13:Amount>115.69</v13:Amount></v13:TotalNetCharge><v13:TotalRebates><v13:Currency>USD</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalRebates><v13:Surcharges><v13:SurchargeType>FUEL</v13:SurchargeType><v13:Description>Fuel</v13:Description><v13:Amount><v13:Currency>USD</v13:Currency><v13:Amount>17.23</v13:Amount></v13:Amount></v13:Surcharges></v13:ShipmentRateDetail></v13:RatedShipmentDetails><v13:RatedShipmentDetails><v13:ShipmentRateDetail><v13:RateType>RATED_ACCOUNT_SHIPMENT</v13:RateType><v13:RateScale>0000000</v13:RateScale><v13:RateZone>GB001O</v13:RateZone><v13:PricingCode>ACTUAL</v13:PricingCode><v13:RatedWeightMethod>ACTUAL</v13:RatedWeightMethod><v13:CurrencyExchangeRate><v13:FromCurrency>UKL</v13:FromCurrency><v13:IntoCurrency>UKL</v13:IntoCurrency><v13:Rate>1.0</v13:Rate></v13:CurrencyExchangeRate><v13:DimDivisor>0</v13:DimDivisor><v13:FuelSurchargePercent>17.5</v13:FuelSurchargePercent><v13:TotalBillingWeight><v13:Units>KG</v13:Units><v13:Value>1.0</v13:Value></v13:TotalBillingWeight><v13:TotalBaseCharge><v13:Currency>UKL</v13:Currency><v13:Amount>62.0</v13:Amount></v13:TotalBaseCharge><v13:TotalFreightDiscounts><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalFreightDiscounts><v13:TotalNetFreight><v13:Currency>UKL</v13:Currency><v13:Amount>62.0</v13:Amount></v13:TotalNetFreight><v13:TotalSurcharges><v13:Currency>UKL</v13:Currency><v13:Amount>10.85</v13:Amount></v13:TotalSurcharges><v13:TotalNetFedExCharge><v13:Currency>UKL</v13:Currency><v13:Amount>72.85</v13:Amount></v13:TotalNetFedExCharge><v13:TotalTaxes><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalTaxes><v13:TotalNetCharge><v13:Currency>UKL</v13:Currency><v13:Amount>72.85</v13:Amount></v13:TotalNetCharge><v13:TotalRebates><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalRebates><v13:Surcharges><v13:SurchargeType>FUEL</v13:SurchargeType><v13:Description>Fuel</v13:Description><v13:Amount><v13:Currency>UKL</v13:Currency><v13:Amount>10.85</v13:Amount></v13:Amount></v13:Surcharges></v13:ShipmentRateDetail></v13:RatedShipmentDetails></v13:RateReplyDetails><v13:RateReplyDetails><v13:ServiceType>INTERNATIONAL_ECONOMY</v13:ServiceType><v13:PackagingType>YOUR_PACKAGING</v13:PackagingType><v13:DeliveryStation>DKKA </v13:DeliveryStation><v13:DeliveryDayOfWeek>MON</v13:DeliveryDayOfWeek><v13:DeliveryTimestamp>2012-12-17T16:30:00</v13:DeliveryTimestamp><v13:CommitDetails><v13:CommodityName>DOCUMENTS</v13:CommodityName><v13:ServiceType>INTERNATIONAL_ECONOMY</v13:ServiceType><v13:CommitTimestamp>2012-12-17T16:30:00</v13:CommitTimestamp><v13:DayOfWeek>MON</v13:DayOfWeek><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:BrokerToDestinationDays>0</v13:BrokerToDestinationDays><v13:CommitMessages><v13:Code>134</v13:Code><v13:Message>REQUEST COMPLETED</v13:Message></v13:CommitMessages><v13:DeliveryMessages> 4:30 P.M. IF NO CUSTOMS DELAY</v13:DeliveryMessages><v13:DocumentContent>DOCUMENTS_ONLY</v13:DocumentContent><v13:RequiredDocuments>INTERNATIONAL_AIRWAY_BILL</v13:RequiredDocuments></v13:CommitDetails><v13:DestinationAirportId>BUF</v13:DestinationAirportId><v13:IneligibleForMoneyBackGuarantee>false</v13:IneligibleForMoneyBackGuarantee><v13:OriginServiceArea>AM</v13:OriginServiceArea><v13:DestinationServiceArea>A4</v13:DestinationServiceArea><v13:SignatureOption>INDIRECT</v13:SignatureOption><v13:ActualRateType>PAYOR_ACCOUNT_SHIPMENT</v13:ActualRateType><v13:RatedShipmentDetails></v13:RatedShipmentDetails><v13:RatedShipmentDetails><v13:ShipmentRateDetail><v13:RateType>RATED_ACCOUNT_SHIPMENT</v13:RateType><v13:RateScale>0000000</v13:RateScale><v13:RateZone>GB001O</v13:RateZone><v13:PricingCode>ACTUAL</v13:PricingCode><v13:RatedWeightMethod>ACTUAL</v13:RatedWeightMethod><v13:CurrencyExchangeRate><v13:FromCurrency>UKL</v13:FromCurrency><v13:IntoCurrency>UKL</v13:IntoCurrency><v13:Rate>1.0</v13:Rate></v13:CurrencyExchangeRate><v13:DimDivisor>0</v13:DimDivisor><v13:FuelSurchargePercent>17.5</v13:FuelSurchargePercent><v13:TotalBillingWeight><v13:Units>KG</v13:Units><v13:Value>1.0</v13:Value></v13:TotalBillingWeight><v13:TotalBaseCharge><v13:Currency>UKL</v13:Currency><v13:Amount>49.7</v13:Amount></v13:TotalBaseCharge><v13:TotalFreightDiscounts><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalFreightDiscounts><v13:TotalNetFreight><v13:Currency>UKL</v13:Currency><v13:Amount>49.7</v13:Amount></v13:TotalNetFreight><v13:TotalSurcharges><v13:Currency>UKL</v13:Currency><v13:Amount>8.7</v13:Amount></v13:TotalSurcharges><v13:TotalNetFedExCharge><v13:Currency>UKL</v13:Currency><v13:Amount>58.4</v13:Amount></v13:TotalNetFedExCharge><v13:TotalTaxes><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalTaxes><v13:TotalNetCharge><v13:Currency>UKL</v13:Currency><v13:Amount>58.4</v13:Amount></v13:TotalNetCharge><v13:TotalRebates><v13:Currency>UKL</v13:Currency><v13:Amount>0.0</v13:Amount></v13:TotalRebates><v13:Surcharges><v13:SurchargeType>FUEL</v13:SurchargeType><v13:Description>Fuel</v13:Description><v13:Amount><v13:Currency>UKL</v13:Currency><v13:Amount>8.7</v13:Amount></v13:Amount></v13:Surcharges></v13:ShipmentRateDetail></v13:RatedShipmentDetails></v13:RateReplyDetails></v13:RateReply></env:Body></soapenv:Envelope>
4

1 回答 1

0
//amount
"/RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Amount"

//currency
"/RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Currency"

看到他们在这里跑

xpath 的来源:文档 示例

更新

foreach ($fedex_xml->xpath("//RateReply//RateReplyDetails") as $details) {

    $amounts = $details->xpath("./RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Amount");
    $currencies = $details->xpath("./RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Currency");

    var_dump($amounts);
    var_dump($currencies);
}

我使用 xpath 表达式来选择 every RateReplyDetails,然后使用其他一些 xpath 表达式来提取值。请注意,./这些开头的部分非常重要,它表示解释当前范围内的 xpath 表达式(RateReplyDetails 节点)。

于 2012-12-12T11:36:55.497 回答