Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.5

Request #10335 Can XML_Feed_Parser get nodeValue within the namespace?
Submitted: 2007-03-12 06:13 UTC
From: ezka1209a Assigned: jystewart
Status: Assigned Package: XML_Feed_Parser (version 1.0.1)
PHP Version: 5.1.6 OS: FreeBSD6.1
Roadmaps: 1.1    
Subscription  


 [2007-03-12 06:13 UTC] ezka1209a (Kazuhiro Iizuka)
Description: ------------ Now I'm trying to get nodeValue within a certain namespace.But currently,this package doesn't support this feature.Do you have any plan to support namespaces? For example,here is the xml file.and I want to get element like this,"$element->{ex:rank}". <?xml version="1.0" encoding="utf-8" ?> <rss version="2.0" xmlns:ex="http://www.example.com/xml/xmlns# "> <channel> <title>Sample</title> <link>http://www.example.com/</link> <description>This is sample xml file.</description> <item> <title>Foo</title> <link>http://www.example.com/bar.html</link> <ex:rank>1</ex:rank> </item> (snip)... </channel> </rss> Now I get like this: $namespace = 'http://www.example.com/xml/xmlns#'; $feed = new XML_Feed_Parser($xml_source); foreach ($feed as $entry) { echo $ranks = $entry->model->getElementsByTagNameNS($namespace, 'rank')->item(0)->nodeValue; } But this way is too long for me.And `echo $e->{ex:rank};' would be intuitively.I wrote the patch to support this notation.Can others agree with my idea?If you have better idea,I want to know. == patch == diff -u Parser.org/RSS2.php Parser/RSS2.php --- Parser.org/RSS2.php Fri Mar 2 14:47:40 2007 +++ Parser/RSS2.php Fri Mar 2 14:47:57 2007 @@ -101,7 +101,7 @@ 'date' => array('pubDate'), 'author' => array('managingEditor')); - protected $namespaces = array( + public $namespaces = array( 'dc' => 'http://purl.org/rss/1.0/modules/dc/', 'content' => 'http://purl.org/rss/1.0/modules/content/' diff -u Parser.org/Type.php Parser/Type.php --- Parser.org/Type.php Wed Feb 28 14:09:01 2007 +++ Parser/Type.php Fri Mar 2 15:03:51 2007 @@ -84,6 +84,10 @@ } } } + if( strstr($call,":") ){ + list($ns_prefix,$method) = explode(":",$call); + return $this->getTextNS($ns_prefix,$method,$arguments); + } if (empty($this->map[$call])) { return false; @@ -241,6 +245,17 @@ } return false; } + protected function getTextNS($ns_prefix,$method, $arguments = array()) + { + $namespace = $this->parent->namespaces[$ns_prefix]; + + $tags = $this->model->getElementsByTagNameNS($namespace,$method); + if ($tags->length > 0) { + $value = $tags->item(0)->nodeValue; + return $value; + } + return false; + } /** * Apply various rules to retrieve category data. --- Parser.php.org Fri Mar 2 15:00:32 2007 +++ Parser.php Fri Mar 2 15:02:18 2007 @@ -181,6 +181,14 @@ /* Instantiate feed object */ $this->feed = new $class($this->model, $strict); + /* Find third party's namespaces */ + if( preg_match_all("|xmlns:(.+)=['\"]([^>]+)['\"]|i",$feed,$matches) ){ + foreach( $matches[1] as $key=>$prefix ){ + if( empty($this->feed->namespaces[$prefix]) ){ + $this->feed->namespaces[$prefix] = $matches[2][$key]; + } + } + } } /** Test script: --------------- $namespace = 'http://www.example.com/xml/xmlns#'; $feed = new XML_Feed_Parser($xml_source); foreach ($feed as $entry) { echo $entry->{"ex:rank"}; } Expected result: ---------------- 1 Actual result: -------------- cannot get any value.

Comments

 [2007-03-12 11:34 UTC] jystewart (James Stewart)
To clarify, this is about supporting arbitrary namespaces. The package already supports a number of namespaces for standard RSS and Atom behaviours and extensions. This is a suggestion to generalise the support, which I am considering.