Protect XML Special Characters in RSS Feeds

Q

How to protect XML Special Characters in RSS Feeds?

✍: FYIcenter.com

A

An RSS document is an XML document. And there 3 primary XML special characters you need to protect them, if you are using them in your RSS element contents:

  • "<" - Used to indicate the start of an XML element tag. It needs to be protected using the escape sequence of "&lt;".
  • ">" - Used to indicate the end of an XML element tag. It needs to be protected using the escape sequence of "&gt;".
  • "&" - Used to indicate the start of an XML escape sequence. It needs to be protected using the escape sequence of "&amp;".

If you are using XML special characters in the "channel/item/description" element, it may get interpreted as HTML tags by some RSS viewers. So you need to apply the HTML escape sequence on top of the XML escape sequence to protect them.

For example, look at how angle brackets are protected in the following RSS feed example, rss-angle-brackets.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
   <channel>
      <title>FYI Center &lt;for&gt; Software Developers</title>
      <link>http://dev.fyicenter.com/</link>
      <description>FYI (For Your Information) Center for Software Developers 
with large collection of FAQs, tutorials and tips.</description>
      <pubDate>Tue, 19 Sep 2017 04:05:38 +0200</pubDate>
      <image>
         <url>http://dev.fyicenter.com/_logo.png</url>
         <title>FYI Center for Software Developers</title>
         <link>http://dev.fyicenter.com/</link>
      </image>
      <atom:link href="http://dev.fyicenter.com/rss_xml.php" rel="self" type="application/rss+xml" />
      <copyright>Copyright (c) FYIcenter.com</copyright> 
      <item>
         <title>Visual Studio &lt;2017&gt; .NET Program Folders</title>
         <link>http://dev.fyicenter.com/1000510_Visual_Studio_2017_NET_Program_Folders.html</link>
         <description>&lt;img align='left' width='64' height='64' 
src='http://dev.fyicenter.com/Visual-Studio/_icon_Dot-NET.png' /&gt;
In which folders Visual Studio 2017 .NET programs are located on my 
Windows computer? Is 1 &amp;lt; 2 or 2 &lt; 1?
- Rank: 110; Updated: 2017-09-19 02:44:09 -> &lt;a 
href='http://dev.fyicenter.com/1000510_Visual_Studio_2017_NET_Program_
Folders.html'&gt;Source&lt;/a&gt;</description>
         <pubDate>Tue, 19 Sep 2017 02:44:09 +0200</pubDate>
         <category>visualstudio</category>
         <guid>http://dev.fyicenter.com/1000510_Visual_Studio_2017_NET_Program_Folders.html</guid>
         <author>info@fyicenter.com (FYIcenter.com)</author>
      </item>
   </channel>
</rss>

The picture below shows you how the above RSS feed is displayed in Internet Explorer:
Protect XML Special Characters in RSS Feeds

If you want to view it in other RSS viewers, you can download it here.

 

RSS specifications

⇒⇒RSS FAQ and Tutorials

2017-11-05, 158👍, 0💬