Pretty simple question, I'm writing an XML document and i'm not sure how to write "]]>" without it being seen as the end of the section.
Asked
Active
Viewed 1,740 times
3 Answers
19
You can do it like this:
]]>]]><
Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
Better answer IMHO than in the duplicate :) – Rory Becker Feb 11 '09 at 19:03
-
1Paolo: think of it that way: CDATA is an area where you don't have to (and in fact can't) escape *anything*. How would you mark the end of such an area? – Joachim Sauer Feb 12 '09 at 15:01
-
1The length (in bytes) could be a required attribute. I absolutely agree with Paolo: Yikes. – Nikolai Ruhe Sep 08 '09 at 14:23
9
I think
<![CDATA[]]]]><![CDATA[>]]>
is the way to go.
That is:
- one CDATA section containing the literal string
]]
(<![CDATA[]]]]>
) - one CDATA section containing the literal string
>
(<![CDATA[>]]>
)
In practice, there would probably be text before the first ]]
and/or after the >
See more at http://en.wikipedia.org/wiki/CDATA#Uses_of_CDATA_sections

IMSoP
- 89,526
- 13
- 117
- 169

Henrik Paul
- 66,919
- 31
- 85
- 96
-
4I don't know if this is the right answer or not, but if so, it's the worst right answer ever. (No offence intended...I'm commenting on the miserableness of the system, rather than your answer.) – Beska Feb 11 '09 at 18:43
5
You can't. CDATA doesn't provide any way to escape characters, so those characters will always represent the end of the CDATA section. You can, however, let them end the CDATA section, add "]]>
" and start a new one with "<![CDATA[
".
This way the String "]]>]]><![CDATA[
" has almost the effect of being an escape for "`]]>" in a CDATA section.

Joachim Sauer
- 302,674
- 57
- 556
- 614