Php 编写正确的xpath查询以删除USPS速率计算器API XML响应

Php 编写正确的xpath查询以删除USPS速率计算器API XML响应,php,xpath,simplexml,usps,Php,Xpath,Simplexml,Usps,我从使用USPS费率计算器Web工具API的查询中得到了以下示例/示例响应: <service id="15"> <pounds>0</pounds> <ounces>15</ounces> <mailtype>Package</mailtype> <container>RECTANGULAR</container> <size>LARG

我从使用USPS费率计算器Web工具API的查询中得到了以下示例/示例响应:

<service id="15">
    <pounds>0</pounds>
    <ounces>15</ounces>
    <mailtype>Package</mailtype>
    <container>RECTANGULAR</container>
    <size>LARGE</size>
    <width>2</width>
    <length>13</length>
    <height>13</height>
    <girth>30</girth>
    <country>ARGENTINA</country>
    <postage>16.25</postage>
    <extraservices>
        <extraservice>
            <serviceid>6</serviceid>
            <servicename>Certificate of Mailing</servicename>
            <available>True</available>
            <price>1.30</price>
        </extraservice>
        <extraservice>
            <serviceid>0</serviceid>
            <servicename>Registered Mail</servicename>
            <available>True</available>
            <price>13.65</price>
        </extraservice>
    </extraservices>
    <valueofcontents>32.25</valueofcontents>
    <inscomment>SERVICE</inscomment>
    <svccommitments>Varies by destination</svccommitments>
    <svcdescription>First-Class Package International Service&amp;lt;sup&amp;gt;&amp;#8482;&amp;lt;/sup&amp;gt;**</svcdescription>
    <maxdimensions>Other than rolls: Max. length 24", max length, height and depth (thickness) combined 36"&lt;br&gt;Rolls: Max. length 36". Max length and twice the diameter combined 42"</maxdimensions>
    <maxweight>4</maxweight>
</service>
</package>
</intlratev2response>
当我使用var_dump$rate时,返回一个空数组。我已经胡闹了好几个小时了。我需要一些指导。如果您需要更多信息,请告诉我,并提前感谢您的帮助。

这里有两个问题:

XML和XPath区分大小写。您需要使用匹配的大小写。 您试图匹配的是价值一流的国际套餐服务,但您试图匹配的价值不仅仅包含&;书信电报;监督及监察;燃气轮机&;8482;&;lt/监督及监察;燃气轮机; 请试试这个:

$path = '//service[starts-with(svcdescription, "First-Class Package International Service")]/postage';
$rate = $result->xpath(path);

您提出的第一点是浏览器渲染的功能。USPSAPI是用Pascal Case编写的,我的代码也是如此。XML示例是从Chrome的开发工具复制为HTML快捷方式复制的。至于你的第二点,在阅读你的解决方案之前,我试着用start with做一个谓词。它似乎也不起作用!我会用你建议的谓词试试。除了你提到的大写问题实际上不是一个问题之外,你的代码工作正常。
$path = '//service[starts-with(svcdescription, "First-Class Package International Service")]/postage';
$rate = $result->xpath(path);