xpath & namespaces
Posted: Sun May 10, 2009 6:00 pm
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:
So, here is what I was trying to do before:
$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?
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>
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
}