Pergunta

I have this sample/example response from a query using USPS Rate Calculator Web Tool 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>

This is a section of the whole response. There are two other service elements, but this is the node that I wish to return. (I left the parent closures in at the bottom for you to see, FYI.) The ID for these different service elements seem static, but there is nothing in the API to indicate that these are intended to be usable. In short, I am reticent to rely on them. The essential identifier for this node is that the SvcDescription = First-Class Package International Service. There is some html encoded hooey that follows, but this is the element I'm trying to locate, and store the parent of it in a SimpleXMLElement.

This is my best shot at the xpath:

$rate = $result->xpath('//Service[SvcDescription="*First-Class Package International Service"]/Postage');

When I var_dump($rate), I am returning an empty array. I have been messing around with this for hours. I could use some guidance. Please let me know if you need more information, and thank you in advance for your help.

Foi útil?

Solução

Two problems here:

  • XML and XPath are case sensitive. You need to use matching capitalization.
  • You are trying to match on the value "First-Class Package International Service", but the value you are trying to match contains more than just that (it also contains "&amp;lt;sup&amp;gt;&amp;#8482;&amp;lt;/sup&amp;gt;")

Please try this:

$path = '//service[starts-with(svcdescription, "First-Class Package International Service")]/postage';
$rate = $result->xpath(path);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top