Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CDATA inside another CDATA

Tags:

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.

like image 386
aborted Avatar asked Oct 12 '12 14:10

aborted


People also ask

Which sequence is not allowed within a CDATA section?

The only sequence which is not allowed within a CDATA section is the closing sequence of a CDATA section itself, ]]> . Note: CDATA sections should not be used within HTML they are considered as comments and not displayed.

What does <![ CDATA in XML mean?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

Is CDATA deprecated?

Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text.


1 Answers

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.

like image 190
raina77ow Avatar answered Sep 21 '22 17:09

raina77ow