26

I have this difficult situation where I need to use the CDATA tags inside another CDATA tags. The situation is simple to explain though.

I have the following thing:

<edit>
<![CDATA[
<script type="text/javascript">
<![CDATA[
    window.onload = function() 
    {
        document.getElementById('block').onclick = function() 
        {
            this.onclick = '';
            this.value = '{LA_SEND_CONFIRM}';
            this.className = this.className.replace('button1','');
            document.getElementById('replacement').value = '{LA_BLOCK_CODE}';
        }
    }
]]>
</script>
]]>
</edit>

I need to wrap my Javascript inside CDATA too for showing purposes, so when I open that XML file, it shows up properly and the Javascript code is inside those CDATA tags. They have no real meaning inside the XML file itself.

As you already know, the code above would give me an XML parsing error, as nesting CDATA wouldn't work. Is there a way to escape the ]]> so I can show those brackets to my users?

I hope I was clear enough.

aborted
  • 4,481
  • 14
  • 69
  • 132
  • 1
    Why can't you have a single `CDATA` section wrapping the whole ` – Oded Oct 12 '12 at 14:04
  • 1
    "A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.": http://www.w3schools.com/xml/xml_cdata.asp – dani herrera Oct 12 '12 at 14:06
  • @Oded I want to show `<![CDATA[ JS here ]]>` to my users as a string. So when someone opens my XML file, they can see the CDATA section for the Javascript only. I thought I explained that already. – aborted Oct 12 '12 at 14:06
  • You may find some help in [this existing question](http://stackoverflow.com/questions/6910361/what-is-the-prefered-way-handle-cdata-in-a-cdata-block) and my answer to it. – Matt Gibson Oct 12 '12 at 14:08
  • 1
    Possible duplicate of [Is there a way to escape a CDATA end token in xml?](http://stackoverflow.com/questions/223652/is-there-a-way-to-escape-a-cdata-end-token-in-xml) – Michael Freidgeim Mar 29 '16 at 12:32

1 Answers1

54

You can escape ]]> substring in CDATA section by replacing it with:

]]]]><![CDATA[>

... line. With this you'll make ]] a part of one CDATA section, and > - of another, that starts right when the preceding one ends.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • neat trick! @Juampy you'll obtain `]]>` using a concatenation of CDATA closing+opening tags: `<![CDATA[` `text]]` `]]>` `<![CDATA[` `>` `text` `]]>` beomes `text]]` `>text` – caesarsol May 20 '15 at 17:34
  • @Juampy two adjacent CDATA sections are displayed as if they were one CDATA section, since there is nothing in between them. The "]]" is part of the first and the ">" is part of the second, so the XML parser never saw an _embedded_ "]]>". – Jesse Chisholm Nov 11 '15 at 18:36
  • https://stackoverflow.com/questions/223652/is-there-a-way-to-escape-a-cdata-end-token-in-xml : <![CDATA[Certain tokens like ]]]]><![CDATA[> can be difficult and ]]> – qxo Jul 20 '18 at 09:26
  • I swear I remember reading somewhere that that particular three-character sequence was impossible to include in a CDATA section, and I thought that was a rather glaring oversight. I guess that source must have been wrong, unless it was like that in an old version of the standard or something. – flarn2006 May 14 '21 at 23:09
  • Strictly speaking, the source was right, because now you have two CDATAs instead of one. In can affect some parsers, I can imagine (because those CDATAs are visible in xml nodes tree) – Krypt Aug 08 '21 at 23:45