Search code examples
xmlcdata

what does <![CDATA[<![CDATA[some text]]]]><![CDATA[>]]> in XML mean?


I am new to XML and its related technologies.

This CDATA tag always comes at the beginning, and then followed by some stuff

I have use this in my XML file:

<text><![CDATA[<![CDATA[TAX INVOICE]]]]><![CDATA[>]]></text>

I have following question. Please help me.

what does <![CDATA[some text]]]]> means?


Solution

  • Unless there are special characters such as "&" and "<" in the content, the string

    <![CDATA[xxxxx]]>
    

    means exactly the same as

    xxxxx
    

    The difference is that in the second form, "&" and "<" have a special meaning, while in the CDATA form, the only thing with a special meaning is the string "]]>" which acts as a terminator.

    Your more complex example:

    <![CDATA[<![CDATA[TAX INVOICE]]]]><![CDATA[>]]>
    

    is a bit of a nightmare, and results from a sloppy programming habit of wrapping text in CDATA sections out of laziness. CDATA sections cannot be nested, so the first ]]> terminates the first <!CDATA[, which means that the string is equivalent to

    <![CDATA[TAX INVOICE]]>
    

    You might think that this in turn is equivalent to

    TAX INVOICE
    

    but that is not the case, because an XML parser will only interpret the outer CDATA delimiters, and the content it will pass to the application is therefore

    <![CDATA[TAX INVOICE]]>