Encoding XML in Movable Type Templates

Last week, I helped Jessamyn West fix a problem with the XML encoding of the RSS 2.0 feed on the Movable Type weblog she ran during the Democratic National Convention.

West's feed couldn't be read successfully by the Convention Bloggers aggregator because of the following line:

[more]

This line produced output of this form:

some entry text]]> [more]

The block that begins with and ends with ]]> is character data in XML. The encode_xml attribute causes this encoding to take place, which ensures that text has been formatted properly for transmission as XML data.

The problem was caused by text appearing outside of the CDATA block. Though it's valid XML, by my understanding, some XML parsers cannot handle element content that appears outside of a CDATA block or content that includes more than one CDATA block.

If you understand how to create a CDATA block, you can solve this problem in a Movable Type template by dropping the encode_xml attribute and creating the CDATA block by yourself, as in this example:


[more] ]]>

For West's weblog, I used this technique to add another feature she wanted -- the full text of extended weblog entries in the feed:





]]>

Comments

One important difference between MT doing CDATA sections and you hardcoding them in a template is that MT checks the contents for the closing characters, and escapes the greater-than if it happens to find them. I don't really expect that Jessamyn often uses those three characters in a row (though some parser I was using the other day was being even more touchy than that: I've forgotten just what it was calling a CDATA end tag, but something like two square brackets followed by whitespace followed by characters followed by a greater-than).

Sure hope the folks who insisted on having MT do CDATA escaping rather than entity escaping for aesthetic reasons are enjoying their pretty, pretty XML feeds :)

Heh. My Radio bug report from 18 months ago, with 610 reads and no response. Roughly the same success I've had fighting CDATA in MT, though there at least I got a config option nobody sees.

To expand a bit on what Phil's referring to, here's the relevant section of the MT.cfg file which addresses the problem:

"By default, when encoding data for XML Movable Type checks whether your data looks like it contains any HTML tags or other unsafe-for-XML data, and if so, it encloses your data in CDATA tags. Some news aggregators have trouble with combinations of CDATA with other data, though, so if this is causing a problem for you, you can use NoCDATA to encode any special characters into entities."

And to fix it, you simply put the line "NoCDATA 1" by itself in your mt.cfg file. This change was made for version 2.61, released February 16, 2003:

http://www.movabletype.org/docs/mtchanges.html#2.61%20(2003.02.16)

Thanks for the tip. I had forgotten that configuration setting. It wouldn't have helped me here -- I didn't have shell access to the server -- but it's preferable to creating your own CDATA blocks.

Add a Comment

All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).