xpath & namespaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jon23d
Forum Newbie
Posts: 10
Joined: Wed Jun 07, 2006 3:47 am

xpath & namespaces

Post by jon23d »

I'm having some trouble with XML namespaces & xpath. I've come up with a workaround, but surely there is a better way.

The XML comes from the categories api of ebay. Here is a small sample of it:

Code: Select all

 
  <?xml version="1.0" encoding="UTF-8"?>
  <GetCategoriesResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2009-05-10T17:53:51.069Z</Timestamp>
    <Ack>Success</Ack>
    <Version>615</Version>
    <Build>e615__Bundled_8694629_R1</Build>
    <CategoryArray>
      <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <CategoryID>6000</CategoryID>
        <CategoryLevel>1</CategoryLevel>
        <CategoryName>eBay Motors</CategoryName>
        <CategoryParentID>6000</CategoryParentID>
        <LSD>true</LSD>
      </Category>
  </GetCategoriesResponse>
 
So, here is what I was trying to do before:

Code: Select all

 
$selected_category = $_GET['category'];
$categories_xml = simplexml_load_file($_APPLICATION['ebay_categories_cache_filename']);
$xml_category_path = "/GetCategoriesResponse/CategoryArray/Category[CategoryID=$selected_category]";
$matching_categories = $categories_xml->xpath($xml_category_path);
if ($matching_categories) {
  echo "Found {$matching_categories[0]->CategoryName}<br />";
} elseif ($matching_categories == false) {
  echo "Returned false!";
} else {
  // dumped variables here
}
 
$matching_categories was returning false. The only way I can figure out how to make it work is by reading the file into a string and replacing the namespace declaration with nothing, then it works fine... I tried using registerXPathNamespace, but it doesn't seem to do anything but throw errors after I try to run xpath()... Am I missing something?
Last edited by Benjamin on Sun May 10, 2009 9:38 pm, edited 1 time in total.
Reason: Changed code tag type from text to xml, php.
Post Reply