Use CDATA Around HTML in description Elements


Use CDATA Around HTML in <description> Elements

Take a look at this feed, in which the <description> elements for both the channel and the RSS item include HTML.

    <?xml version="1.0" encoding="ISO-8859-1"?>    <rss version="2.0">     <channel>      <generator>NewzAlert Composer v1.70.6, Copyright (c) 2004-2005      Castle Software Ltd, http://www.NewzAlert.com</generator>      <lastBuildDate>Thu, 08 Dec 2005 14:01:27 -0500</lastBuildDate>      <pubDate>Thu, 08 Dec 2005 14:01:34 -0500</pubDate>      <title>Steve's News</title>      <description>This feed contains <b>news        from Steve</b>!</description>      <link>http://www.rssmaniac.com.com/steve</link>      <language>en-us</language>      <copyright>(c) 2006</copyright>      <managingEditor>Steve@rssmaniac.com</managingEditor>      <image>       <title>Steve's News</title>       <url>http://www.rssmaniac.com/steve/Image.jpg</url>       <link>http://www.rssmaniac.com/steve</link>       <description>Steve's News</description>       <width>144</width>       <height>36</height>      </image>        <item>         <title>Steve shovels the snow</title>         <description>It snowed once again.           <br>Time to shovel!</description>         <pubDate>Thu, 08 Dec 2005 08:39:51 -0500</pubDate>         <link>http://www.rssmaniac.com/steve</link>        </item>        <textinput>          <title>Do a Search</title>          <description>What do you want to find?</description>          <name>search</name>          <link>http://www.rssmaniac.com/find.cgi</link>        </textinput>     </channel>    </rss> 


When you include HTML in your RSS feed's <description> element this way, you take the risk that the result will not be bona fide XML, as in this example. (This is true for any HTML you use in the <title> elementbut you should avoid using HTML in the <description> element.)

Creating malformed RSS files is a big problem, because some RSS readers might not be able to read your feed, and you might not even know there's a problem unless you test all the RSS readers available.

One solution is to enclose the description text in an XML CDATA section. While XML documents are usually parsed thoroughly, XML parsers skip the text in CDATA sections. In other words, you can put any non-XMLconforming text inside a CDATA section, and the parser won't choke. Your RSS will be valid.

To create a CDATA section, you enclose the sensitive text in CDATA markup, which starts with <![CDATA[ and ends with ]]>. Some RSS creator programs, like NewzAlert Composer, routinely place all text in the <description> elements inside CDATA sections. Here's how that works with NewzAlert Composer, where all the sensitive text, including the HTML in that text, is stored inside CDATA sections:

     <?xml version="1.0" encoding="ISO-8859-1"?>     <rss version="2.0">      <channel>       <generator>NewzAlert Composer v1.70.6, Copyright (c) 2004-2005       Castle Software Ltd, http://www.NewzAlert.com</generator>       <lastBuildDate>Thu, 08 Dec 2005 14:01:27 -0500</lastBuildDate>       <pubDate>Thu, 08 Dec 2005 14:01:34 -0500</pubDate>       <title>Steve's News</title>              <description><![CDATA[This feed contains <b>news        from Steve</b>!]]></description>       <link>http://www.rssmaniac.com.com/steve</link>       <language>en-us</language>       <copyright>(c) 2006</copyright>       <managingEditor>Steve@rssmaniac.com</managingEditor>       <image>        <title>Steve's News</title>        <url>http://www.rssmaniac.com/steve/Image.jpg</url>        <link>http://www.rssmaniac.com/steve</link>        <description>Steve's News</description>        <width>144</width>        <height>36</height>       </image>         <item>         <title>Steve shovels the snow</title>                   <description><![CDATA[It snowed once again.           <br>Time to shovel!]]></description>         <pubDate>Thu, 08 Dec 2005 08:39:51 -0500</pubDate>         <link>http://www.rssmaniac.com/steve</link>         </item>         <textinput>           <title>Do a Search</title>           <description>What do you want to find?</description>           <name>search</name>           <link>http://www.rssmaniac.com/find.cgi</link>         </textinput>      </channel>     </rss> 


This raises another issue, however: Are there RSS readers that will choke on CDATA sections? That's possible, but after using about two dozen RSS readers, I haven't found any.

Tip

Having trouble with HTML in <description> elements? Solution: Don't use HTML in descriptions at all. Plenty of RSS readers either have trouble with HTML or strip it out. Save the fireworks for the article associated with the RSS item on your Web site.




Secrets of RSS
Secrets of RSS
ISBN: 0321426223
EAN: 2147483647
Year: 2004
Pages: 110

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net