technology from back to front

XML CDATA and escaping

XML’s syntax for CDATA looks like this:

  <![CDATA[some text]]>

Tag syntax within a CDATA section is suspended, so this is well-formed XML:

  <![CDATA[some <more> text]]>

even though it looks like the “<more>” tag is unclosed.

There’s only one thing you can’t say in a CDATA section: “]]>”. But there’s a trick to save us, even here. To print an arbitrary string in a CDATA enclosure, replace each instance of “]]>” with “]]]]><![CDATA[>", and then put the normal "<![CDATA["/"]]>” brackets around it:

  my ]]> text

becomes

  <![CDATA[my ]]]]><![CDATA[> text]]>
by
tonyg
on
25/10/07
  1. I’m (pleasantly) surprised that Google Feed Reader actually formatted your post correctly!

  2. thanks.

  3. [...] your I want <strong>HTML here</strong> with CDATA tags, as [...]

  4. [...] XML CDATA and escaping « LShift Ltd.XMLCDATA. Syntax. Description of XMLCdata.gif follows. Description of the illustration XMLCdata.gif. Purpose. XMLCData generates a CDATA section by … [...]

 
 


one + = 7

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us