Search code examples
xmlxsltcdata

I don't understand the idea of CDATA


I have a client requirement:

         <account>
         <Salutation><![CDATA[Dear Customer]]></Salutation>
         </account>

I don't understand why they mentioned this. When I try to display it with

         <xsl:value-of select="account/Salutation" /> 

I am getting the output as normal Dear Customer. If this is the required output then why is CDATA mentioned? Or should it produce [Dear Customer] - output with []?


Solution

    1. <![CDATA allows an XML processor to skip over until the occurrence of ]]> This can be useful in many situations, e.g. like in your example: to transport user generated data within an XML envelop.

      The data in between the CDATA section must not follow XML encoding rules and therefore can be transported as is. But there are other use cases as well:

    2. In your example the transported data is:Dear Customer. The <![CDATA[]] is just for the XML processor.