Page 1 of 1

Xpath trouble

Posted: Sun May 06, 2007 12:58 pm
by tores
Sorry for posting this here, but I suspect there are gurus in the php community who knows the answer. Googling has not gotten me anywhere.. I don't know.. maybe I'm asking the wrong questions...

Basically, it's an xpath problem: I need to query some itunes tags in an rss. They are typically named <itunes:[something]>. The problem is that the following xpath query is invalid "rss/channel/itunes:[something]". And it's the colon that breaks it... I've tried looking for some way to escape the colon, without success. Maybe I'm taking the wrong approach as using colon's in xml tags is a typical namespace thing.... Well, I'm out of ideas, so any help is appreciated...

Regards, tores

Posted: Sun May 06, 2007 3:18 pm
by Weirdan
As far as I know you need to provide namespace->prefix mapping to your xpath query engine. How you do that depends on the particular engine you're using. So... post your code :)

Posted: Sun May 06, 2007 6:33 pm
by volka
Using "An Example Feed" from http://www.apple.com/itunes/store/podca ... c526931674 as source

Code: Select all

<?php
$dom = DOMDocument::load('test.xml');
$xpath = new DOMXPath($dom);

$nodeset = $xpath->query('//rss/channel/itunes:summary');
if ( 0<$nodeset->length ) {
	echo $nodeset->item(0)->textContent;
}

echo "\n<hr />\n";

$nodeset = $xpath->query('//rss/channel/itunes:*');
foreach($nodeset as $node) {
	echo $node->nodeName, "<br />\n";
}
prints
All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our Podcast in the iTunes Music Store
<hr />
itunes:subtitle<br />
itunes:author<br />
itunes:summary<br />
itunes:owner<br />
itunes:image<br />
itunes:category<br />
itunes:category<br />

Posted: Mon May 07, 2007 8:44 am
by tores
Registering the itunes namespace dtd solved the problem (I'm not using PHP, but rather a Python wrapper for libxml2)