SimpleXML and registerXPathNamespace
| From: | Mario Caprino | Date: | Fri, 13 Jun 2008 14:32:54 +0000 |
| Subject: | SimpleXML and registerXPathNamespace | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-275332@lists.php.net to get a copy of this message | ||
Hi,
I am having problems with the below test case. I am using an extended
googlebase Atom format to store my data. Included in the test case is
a simplified version of my XML-document.
PROBLEM:
Why do I need to re-register namespace 'a' when using xpath through
the result of a previous xpath()-statement?
As you may notice the same is not true for namespace 'g'.
I can't get this to make sense!
Any insight would be much appreciated.
Best regards,
Mario Caprino
<?php
#Simpliefied sample data
$str= <<<EOF
<?xml version="1.0" encoding="iso-8859-1"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:g="http://base.google.com/ns/1.0"
xml:lang="en">
<entry>
<g:mpn>10100</g:mpn>
<title>My product</title>
<title xml:lang="no">Mitt produkt</title>
<g:price>299</g:price>
</entry>
</feed>
EOF;
#Load document
$xml= simplexml_load_string ($str);
$xml->registerXPathNamespace ('a', 'http://www.w3.org/2005/Atom');
$xml->registerXPathNamespace ('g', 'http://base.google.com/ns/1.0');
echo "This is what I am trying to achieve...\n";
$lang= 'no';
$id= '10100';
$title= $xml->xpath
("/a:feed/a:entry[g:mpn='$id']/a:title[lang('$lang')]");
echo utf8_decode ($title[0]) . "\n";
#---
echo "But I'd fist like to store the entity node, and then request
several data nodes relative to it\n";
$entry= $xml->xpath ("/a:feed/a:entry[g:mpn='$id']");
$entry= &$entry[0];
$price= $entry->xpath ("g:price");
echo "This works as expected: $price[0]\n";
#$entry->registerXPathNamespace ('a', 'http://www.w3.org/2005/Atom');
$title= $entry->xpath ("a:title[lang('$lang')]");
echo '... but this will only work if I re-register the namespace "a"?
' . utf8_decode ($title[0]);
?>