Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
768 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...