homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Add rss xmlns namespace definition to a php simplexml document?
Feedvalidator doesn't like xlmsn before xml type

5+ Year Member

Msg#: 4116252 posted 10:02 am on Apr 15, 2010 (gmt 0)

I'm trying to create an itunes-valid podcast feed using php5's simplexml:

$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
$xml_generator = new SimpleXMLElement($xml_string);
$tnsoundfile = $xml_generator->addChild('title', 'Main Title');
$tnsoundfile->addChild('itunes:author', "Author", ' ');
$tnsoundfile->addChild('category', 'Audio Podcasts');
$tnsoundfile = $xml_generator->addChild('item');
$tnsoundfile->addChild('title', 'The track title');
$enclosure = $tnsoundfile->addChild('enclosure');
$enclosure->addAttribute('url', 'http://test.com');
$enclosure->addAttribute('length', 'filelength');
$enclosure->addAttribute('type', 'audio/mpeg');
$tnsoundfile->addChild('itunes:author', "Author", ' ');
header("Content-Type: text/xml");
echo $xml_generator->asXML();

It doesn't validate, because I've got to put the line:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

as per [apple.com...]

So the output SHOULD be:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

etc. I've been over and over the manual and forums, just can't get it right. If I put, near the footer:

header("Content-Type: text/xml");
echo '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">';
echo $xml_generator->asXML();

Then it sort of looks right in firefox and it doesn't complain about undefined namespaces anymore, but feedvalidator complains that

line 1, column 77: XML parsing error:
<unknown>:1:77: xml declaration not at
start of external entity [help]

because the document now starts:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><?xml version="1.0" encoding="UTF-8"?>

and not

<?xml version="1.0" encoding="UTF-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

Any advice gratefull received!



5+ Year Member

Msg#: 4116252 posted 7:56 pm on Apr 17, 2010 (gmt 0)

Well, the conclusion is that php simplexml isn't capable of producing
an itunes-store-compliant podcast as specced at:
So, here's the workaround:
$xml_result = $xml_generator->asXML();
$xml_LeftHandSide = '<?xml version="1.0" encoding="UTF-8"?><rss
$xml_RightHandSide = substr($xml_result, 22);
echo $xml_LeftHandSide . $xml_RightHandSide . "</rss>";
et voila! feedvalidator and itunes are happy :)
Wish I'd thought of this 5 nights ago...


WebmasterWorld Senior Member 10+ Year Member

Msg#: 4116252 posted 9:08 pm on Apr 17, 2010 (gmt 0)

I've never understood why people bother with the built in XML functions in PHP. It's so easy to just generate and parse using custom code... Which, although not compiled like native PHP functions, does so much less processing that it's actually faster.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved