Reputation: 253
I am building a RSS feed in PHP but I am bit confuse with namespace Here is my code
<?php
header("Content-Type: application/rss+xml; charset=UTF-8");
$xml = new SimpleXMLElement('<rss></rss>', 0 , false , 'media' , true);
$xml->addAttribute("version", "2.0");
$xml->addAttribute("xmlns:xmlns:media" ,"http://search.yahoo.com/mrss/");
$channel = $xml->addChild("channel");
$channel->addChild("title", "xxxxxx.fr");
$channel->addChild("link", "http://www.xxxxxx.fr");
$channel->addChild("description", "XXXXXXX");
$channel->addChild("language", "fr-fr");
foreach($data as $key => $entry){
$item = $channel->addChild("item");
$guid = $item->addChild("guid", $entry['video_id']);
$guid->addAttribute('isPermalink' , 'false');
$item->addChild("title", "<![CDATA[ " .htmlspecialchars($entry['title']). " ]]>");
$title = $item->addChild('media:title', "<![CDATA[ " .htmlspecialchars($entry['title']). " ]]>" , 'media');
$title->addAttribute('type' ,'plain');
$pubdate = $item->addChild("pubDate" , $entry['created_time']->format('Y-m-d H:i:s'));
$media = $item->addChild('media:content', null , 'media');
$media->addAttribute('duration', $entry['duration']);
$media->addAttribute('url', $entry['source_video']);
$thumbnail = $media->addChild('media:thumbnail' , null , 'media');
$thumbnail->addAttribute('height' , '1080');
$thumbnail->addAttribute('width' , '1080');
$thumbnail->addAttribute('url', $entry['picture_path']);
$category = $media->addChild('media:category', $iab , 'media');
$category->addAttribute('scheme' , 'https://support.aerserv.com/hc/en-us/articles/207148516-List-of-IAB-Categories');
}
echo preg_replace('/xmlns:xmlns="media"\s?/', '', $xml->asXML($page_name.'-2.xml'));
?>
And the output
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>xxxx.fr</title>
<link>http://www.xxxx.fr</link>
<description>
XXXXX
</description>
<language>fr-fr</language>
<item>
<guid isPermalink="false">1947196298709465</guid>
<title>
<![CDATA[ Cette artiste utilise la gélatine comme support pour créer des œuvres en 3 dimensions ]]>
</title>
<media:title xmlns:media="media" type="plain">
<![CDATA[ Cette artiste utilise la gélatine comme support pour créer des œuvres en 3 dimensions ]]>
</media:title>
<pubDate>2018-12-10 19:30:01</pubDate>
<media:content xmlns:media="media" duration="216.723" url="https://scontent.xx.fbcdn.net/v/t66.18014-6/10000000_2xxxxx_861713768647979845_n.mp4?_nc_cat=100&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_ht=scontent.xx&oh=9cfb72ebc8fc2bdfd8ae7816899b29e2&oe=5CAC9FBC">
<media:thumbnail height="1080" width="1080" url="https://scontent.xx.fbcdn.net/v/t15.5256-10/47695217_1947199588709136_5040968xxxxx1952_n.jpg?_nc_cat=1&_nc_ht=scontent.xx&oh=ea3af9f82426e5a3f9ca1a3e907b09ca&oe=5CAE1DBB"/>
<media:category scheme="https://support.aerserv.com/hc/en-us/articles/207148516-List-of-IAB-Categories">IAB8</media:category>
</media:content>
</item>
</channel>
</rss>
What I want is remove all xmlns:media="media" expected in the rss node Maybe I did something wrong with namespace I don't know why it add xmlns:media="media" in media:title or media:content
Upvotes: 0
Views: 882
Reputation: 57121
SimpleXML isn't very good at producing clean XML when namespaces are concerned, although you are adding the elements slightly incorrectly...
$media = $item->addChild('media:content', null , 'media');
the third parameter needs to be the URI and not the prefix...
$media = $item->addChild('media:content', null , "http://search.yahoo.com/mrss/");
The second part (although needs to be done first) is how to add the namespace. Once the namespace is defined 'properly' in the XML hierarchy, SimpleXML is OK about not re-declaring it, but as the way it's added at the moment using addAttribute()
isn't actually declaring it, more adding an attribute with the setting...
$xml->addAttribute("xmlns:xmlns:media" ,"http://search.yahoo.com/mrss/");
A way to fake this is to add an attribute to the root element with the new namespace required and then remove it. Fortunately SimpleXML doesn't clear out the declaration and this is now set for the entire document...
$xml->addAttribute("media:a", "temp", "http://search.yahoo.com/mrss/");
unset($xml->attributes("media", true)["a"]);
You can also remove the last replace...
echo $xml->asXML($page_name.'-2.xml'));
Upvotes: 1