SuperGeekery: A blog probably of interest only to nerds by John F Morton.

A blog prob­a­bly of inter­est only to nerds by John Morton.

08Apr2022

RSS feeds and unbound pre­fix errors

I recent­ly was asked to add an image to an exist­ing RSS feed that had been work­ing for a client for years. 

This is the sim­pli­fied ver­sion, with only a sin­gle item, of what I was start­ing 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 Val­i­da­tion Ser­vice, https://​val​ida​tor​.w3​.org/​feed/, you’ll find that this is a valid feed structure.

Adding an image

After a quick look around the inter­net, I decid­ed I need­ed 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 need­ed, doing only that will cause the feed to be invalid. 

For the sake of com­plete­ness, here is the updat­ed exam­ple 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 updat­ed exam­ple fails with the error message:

Rss error

XML parsing error: unbound prefix

The error mes­sage threw me off because it point­ed to the line num­ber where I’d added the image, but that’s not where the fix need­ed to happen. 

Research­ing adding media to an RSS feed led me to this page from 2009: https://​www​.rss​board​.org/​m​e​d​i​a-rss

Media RSS is a new RSS mod­ule that sup­ple­ments the capa­bil­i­ties of RSS 2.0. RSS enclo­sures are already being used to syn­di­cate audio files and images. Media RSS extends enclo­sures to han­dle oth­er media types, such as short films or TV, as well as pro­vide addi­tion­al meta­da­ta with the media. Media RSS enables con­tent pub­lish­ers and blog­gers to syn­di­cate mul­ti­me­dia con­tent such as TV and video clips, movies, images and audio.

And fur­ther down the page, it says:

The name­space for Media RSS is defined to be http://​search​.yahoo​.com/​mrss/

Although the page was­n’t the eas­i­est doc­u­ment to parse, the Media RSS name­space is the answer. 

I need to add the fol­low­ing to my RSS feed.

xmlns:media="http://search.yahoo.com/mrss/"

What the Yahoo?

I was ini­tial­ly thrown by the ref­er­ence to Yahoo. It turns out, accord­ing to the Media RSS page on Wikipedia, that the Media RSS exten­sion was orig­i­nal­ly designed by Yahoo! and the Media RSS com­mu­ni­ty in 2004.” I assume that’s why the dec­la­ra­tion includes yahoo.com.

The dec­la­ra­tion needs to be added to the 2nd line of my exam­ple RSS feed above. Again, to be ver­bose, here’s the updat­ed exam­ple feed with the cor­rect 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 updat­ed exam­ple feed through the val­ida­tor, things look much bet­ter now.

Rss success