29

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.

5
  • 1
    Why can't you have a single CDATA section wrapping the whole <script> element? Commented Oct 12, 2012 at 14:04
  • 2
    "A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.": w3schools.com/xml/xml_cdata.asp Commented Oct 12, 2012 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. Commented Oct 12, 2012 at 14:06
  • You may find some help in this existing question and my answer to it. Commented Oct 12, 2012 at 14:08
  • 1
    Possible duplicate of Is there a way to escape a CDATA end token in xml? Commented Mar 29, 2016 at 12:32

1 Answer 1

57

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.

Sign up to request clarification or add additional context in comments.

5 Comments

neat trick! @Juampy you'll obtain ]]> using a concatenation of CDATA closing+opening tags: <![CDATA[ text]] ]]> <![CDATA[ > text ]]> beomes text]] >text
@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 "]]>".
stackoverflow.com/questions/223652/… : <![CDATA[Certain tokens like ]]]]><![CDATA[> can be difficult and <valid>]]>
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.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.