Embedding HTML: The CDATA Command

Author: John Fowler

Once the text encapsulated within the XML elements is parsed by the RSS reader it is sent to the web browser and displayed as html. If so, why not take advantage of what html can offer and include actual html commands within the feed's elements? There is no reason why not to, but if you choose to do so at least do it the proper way.

XML commands, much like HTML, are comprised of tags. For example, if we wanted to display the text 'RSS ' in a bold font we would use the '<b>' tag:

<b>RSS</b>

Supposedly, had we wanted that to be our description of the item, all we would have done is enter the following line?

<description><b>RSS</b></description>

But that is not the case. The parser would have treated <b> and </b> as tags rather than text that is part of the description data. So 'RSS' will be recognized as part of a 'b' element that is a sub element of the description element.

The way that was initially used to tackle this problem was to use entity-encoded HTML aka double entity-encoding. To implement this method the '<' and '>' chars (as well as a couple of other chars) would be replaced by special bits of code that the parser would recognize as text yet a browser would identify and treat as '<' and '>'.

The way the above example would look using entity-encoded HTML:

<description&lt;&lt;b>RSS&lt;/b&gt;</description>

A little cumbersome for such a short piece of code wouldn't you say? Although the use of entity-encoded html is very common, it can get pretty messy and is not recommended. There is no reason to use it anymore so we won't go into further detail.

The way HTML should be embedded in RSS feeds is using the CDATA command:

<! [CDATA[ html code ]]>

With CDATA you can attach any HTML code without worrying about any of the issues associated with double entity encoding.

Say we'd like to emphasize the 'Martian king' item and have it displayed in a bold font, as we attribute great importance to it:

<description><! [CDATA[<B>The king of Mars will make an historic visit to earth. According to government officials his majesty is expected to arrive some time next month accompanied by a whole bunch of 'greenish little men'</B>]]>.<description>

That was easy! Just slap those bold tags on the text and wrap it all with the CDATA square brackets.

A word of caution: Adding HTML commands should be limited to description elements only. Other elements such as title are considered metadata, and although some readers can identify such cases, and filter any unnecessary html elements, it would be best if those wouldn’t be placed there to begin with.

2006 © Society for Technological Education | Site Map | Contact Us