0

I want to get the ISBN number of a book from an Amazon XML File. I already checked other posts and tried to solve the problem with them but without any success. The $xml looks like:

<itemlookupresponse>
 <items>
  <item>
   <itemattributes>
    <studio>Pottermore from J.K. Rowling</studio>
    <eisbn>9781781100769</eisbn>
   </itemattributes>
  </item>
  <item>
   <itemattributes>
    <studio>Carlsen Verlag GmbH</studio>
    <isbn>3551551677</isbn>
   </itemattributes>
  </item>
  <item>
   <itemattributes>
    <studio>Carlsen</studio>
    <isbn>3551551677</isbn>
   </itemattributes>
  </item>
 </items>
</itemlookupresponse>

I want to get the items, where the ISBN equals 3551551677. For that reason, I used the following command, which unfortunately returns an empty array.

$item = $xml->xpath('//items/item[itemattributes/isbn=3551551677]');

I would be glad, if someone could help me and explain, what I made wrong.

Rurpot
  • 239
  • 2
  • 6
  • Running it with the XML segment you have above returns the expected element. Is there more to the XML document than you have in your question. – Nigel Ren Feb 21 '18 at 12:18
  • The $xml is a SimpleXMLElement Object ... do I have to parse it or convert it, bevor I can apply xpath? – Rurpot Feb 21 '18 at 12:22
  • The only line I've added (assuming data loaded as above in string) is `$xml = simplexml_load_string($data);` – Nigel Ren Feb 21 '18 at 12:23
  • Can you post the actual XML content (pre loading it into SimpleXML) to your question. – Nigel Ren Feb 21 '18 at 13:38

2 Answers2

1

XML and XPath are case-sensitive. Amazon's XML is not all lowercase, despite what you've posted in your question. Adjust your XPath to match the exact case used in Amazon's actual XML.

Note also that if there are namespaces involved in the actual XML, those too must be accounted for in your XPath. See How does XPath deal with XML namespaces?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I already tried it with the right letters ... lowercase and uppercase, there is no different in the result. I guess I have to dig deeper in this namespaces thing. Thanks for your answers. – Rurpot Feb 21 '18 at 13:22
  • If you post an actual [mcve], someone could easily help you with a specific, direct answer. Without a MCVE, you're going to get guesses. – kjhughes Feb 21 '18 at 13:24
0

You didn't show something important (namespaces?) because this code works for me:

$string = <<<XML
<itemlookupresponse>
 <items>
  <item>
   <itemattributes>
    <studio>Pottermore from J.K. Rowling</studio>
    <eisbn>9781781100769</eisbn>
   </itemattributes>
  </item>
  <item>
   <itemattributes>
    <studio>Carlsen Verlag GmbH</studio>
    <isbn>3551551677</isbn>
   </itemattributes>
  </item>
  <item>
   <itemattributes>
    <studio>Carlsen</studio>
    <isbn>3551551677</isbn>
   </itemattributes>
  </item>
 </items>
</itemlookupresponse>
XML;
$xml = new SimpleXMLElement($string);
/* Поиск <a><b><c> */
$items = $xml->xpath('//items/item[ ./itemattributes/isbn[.="3551551677"] ]');
//$items = $xml->xpath('//items/item[itemattributes/isbn=3551551677]');
echo "Result size: " . count($items) . "\n";
foreach ( $items as $item) {

    echo $item->asXML() . "\n";
}
gangabass
  • 10,607
  • 2
  • 23
  • 35
  • @RaymondNijland: Usually true, but here adding an explanation won't help because **this XPath is equivalent, but needlessly more verbose, than OP's original.** This answer should be deleted. – kjhughes Feb 21 '18 at 17:46