doukuizuo1795 2012-12-12 11:18
浏览 46
已采纳

PHP simplexml和xpath访问节点值

I have the below XML response from the UPS Rates API and I need to get the values of 'ServiceType', 'Amount' and 'Currency' nodes for each 'RateReplyDetails' only where the RateType node = 'RATED_ACCOUNT_SHIPMENT' .

My attempt is below, the ServiceType works but not the amount and currency. Can anyone assist?

$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>
  • 写回答

1条回答 默认 最新

  • dongqiu8375 2012-12-12 11:36
    关注
    //amount
    "/RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Amount"
    
    //currency
    "/RatedShipmentDetails/ShipmentRateDetail[RateType = 'RATED_ACCOUNT_SHIPMENT']/TotalNetCharge/Currency"
    

    see them running here

    sources for xpath: doc examples

    Update

    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);
    }
    

    I used a xpath expression to select every RateReplyDetails, then used some other xpath expressions to extract the values. Note that the ./ part at the beginning of these are very important, it says interpret the xpath expression in the current scope (RateReplyDetails node).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?