RSS feeds and unbound prefix errors
I recently was asked to add an image to an existing RSS feed that had been working for a client for years.
This is the simplified version, with only a single item
, of what I was starting with.
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Example Site Name</title>
<atom:link href="http://example.com/feed" rel="self" type="application/rss+xml" />
<link>http://example.com/</link>
<description>News from Example</description>
<language>en-us</language>
<pubDate>Fri, 08 Apr 2022 09:53:25 -0500</pubDate>
<lastBuildDate>Fri, 08 Apr 2022 09:53:25 -0500</lastBuildDate>
<item>
<title>My Example Entry Title</title>
<link>http://example.com/news/my-example-entry</link>
<pubDate>Thu, 17 Feb 2022 16:33:00 -0600</pubDate>
<guid>http://example.com/news/my-example-entry</guid>
<description><![CDATA[Description goes here.]]></description>
</item>
</channel>
</rss>
If you use the W3C Feed Validation Service, https://validator.w3.org/feed/, you’ll find that this is a valid feed structure.
Adding an image
After a quick look around the internet, I decided I needed to add a media:content
line to the item
node to add an image.
<media:content url='https://example.com/image.jpeg' medium='image' width='720' height='405'/>
While adding this line is needed, doing only that will cause the feed to be invalid.
For the sake of completeness, here is the updated example with this line, which will cause the error.
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Example Site Name</title>
<atom:link href="http://example.com/feed" rel="self" type="application/rss+xml" />
<link>http://example.com/</link>
<description>News from Example</description>
<language>en-us</language>
<pubDate>Fri, 08 Apr 2022 09:53:25 -0500</pubDate>
<lastBuildDate>Fri, 08 Apr 2022 09:53:25 -0500</lastBuildDate>
<item>
<title>My Example Entry Title</title>
<link>http://example.com/news/my-example-entry</link>
<pubDate>Thu, 17 Feb 2022 16:33:00 -0600</pubDate>
<guid>http://example.com/news/my-example-entry</guid>
<media:content url='https://example.com/image.jpeg' medium='image' width='720' height='405'/>
<description><![CDATA[Description goes here.]]></description>
</item>
</channel>
</rss>
This updated example fails with the error message:
The error message threw me off because it pointed to the line number where I’d added the image, but that’s not where the fix needed to happen.
Researching adding media to an RSS feed led me to this page from 2009: https://www.rssboard.org/media-rss
Media RSS is a new RSS module that supplements the
capabilities of RSS 2.0. RSS enclosures are already being used to syndicate audio files and images. Media RSS extends enclosures to handle other media types, such as short films or TV, as well as provide additional metadata with the media. Media RSS enables content publishers and bloggers to syndicate multimedia content such as TV and video clips, movies, images and audio.
And further down the page, it says:
The namespace for Media RSS is defined to be http://search.yahoo.com/mrss/
Although the page wasn’t the easiest document to parse, the Media RSS namespace is the answer.
I need to add the following to my RSS feed.
xmlns:media="http://search.yahoo.com/mrss/"
What the Yahoo?
I was initially thrown by the reference to Yahoo. It turns out, according to the Media RSS page on Wikipedia, that the Media RSS extension was “originally designed by Yahoo! and the Media RSS community in 2004.” I assume that’s why the declaration includes yahoo.com
.
The declaration needs to be added to the 2nd line of my example RSS feed above. Again, to be verbose, here’s the updated example feed with the correct declaration.
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Example Site Name</title>
<atom:link href="http://example.com/feed" rel="self" type="application/rss+xml" />
<link>http://example.com/</link>
<description>News from Example</description>
<language>en-us</language>
<pubDate>Fri, 08 Apr 2022 09:53:25 -0500</pubDate>
<lastBuildDate>Fri, 08 Apr 2022 09:53:25 -0500</lastBuildDate>
<item>
<title>My Example Entry Title</title>
<link>http://example.com/news/my-example-entry</link>
<pubDate>Thu, 17 Feb 2022 16:33:00 -0600</pubDate>
<guid>http://example.com/news/my-example-entry</guid>
<media:content url='https://example.com/image.jpeg' medium='image' width='720' height='405'/>
<description><![CDATA[Description goes here.]]></description>
</item>
</channel>
</rss>
If you run this updated example feed through the validator, things look much better now.