Re: [xml-dev] What characters can go into a CDATA section and a comment? (I found inconsistencies)

From
Mukul Gandhi <>
To
XML Developers List <>
Date
2022-03-24T05:06:29Z
ID
<CANGHzgBxTp3wLniLhhiwVHKqnmJr-G11zvO-uNfD8S=qmKe-=>
Thread
Re: [xml-dev] What characters can go into a CDATA section and a comment? (I found inconsistencies)
Hi all,
    I personally see, few similarities between purposes of CDATA section and a comment within the context of XML documents.

I seem to clearly, understand the purpose of comments. Any text within comments, are only for human reading (and rarely for automated reading by computers. for e.g, we have XPath comment nodes).

But I'd like to know, what are the use cases of CDATA sections. Any thoughts, shall be helpful to know.

On Wed, Mar 23, 2022 at 5:09 PM Roger L Costello <[email protected]> wrote:
Hi Folks,

I did some testing to see what characters can go into a CDATA section and a comment. Here's what I found:

Recall the CDATA syntax: <![CDATA[ ... ]]>

Question: Can the content of a CDATA section contain the ] character?

<![CDATA[foo]bar]]>

Answer: Yes

Question: Can the content of a CDATA section contain ]]?

<test><![CDATA[foo]]bar]]></test>

Answer: Yes.

Question: Can the content of a CDATA section contain more than two ] characters at the end?

<![CDATA[foo]]]]]>

Answer: Yes.

Question: Can the content of a CDATA section contain more than one [ character at the start?

<![CDATA[[[foo]]>

Answer: Yes.

Recall the comment syntax: <!-- ... -->

Question: Can the content of a comment contain a dash character?

<!-- foo - bar -->

Answer: Yes.

Question: Can the content of a comment contain two dashes?

<!-- foo -- bar -->

Answer: No.

Question: Can the content of a comment end with more than two dashes?

<!-- foo----->

Answer: No.

Notice the inconsistency: A CDATA section can end with more than two ] characters but a comment cannot end with more than two dashes:

<![CDATA[foo]]]]]>      Okay
<!-- foo----->          Error

Also, a CDATA section can contain ]] but a comment cannot contain --

<![CDATA[foo]]bar]]>
<!-- foo -- bar -->

Why is there this inconsistency?

Isn't inconsistency in a data format language bad?

/Roger


--
Regards,
Mukul Gandhi