Xpath trouble

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
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Xpath trouble

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 />
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

Registering the itunes namespace dtd solved the problem (I'm not using PHP, but rather a Python wrapper for libxml2)
Post Reply