2

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 []?

hat
  • 733
  • 1
  • 17
  • 23

1 Answers1

2
  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.
jschnasse
  • 7,020
  • 4
  • 27
  • 59
  • Thank you. Can you tell me if any Special character is there , how should I give my xsl code – Indu Velayutham Feb 25 '18 at 17:26
  • I have the feeling that this is a new question. You can access CDATA via XSLT with . There is nothing special. – jschnasse Feb 25 '18 at 18:24
  • Oh thanks.. I will try it out and reply you... bcz the client specifically mentioned cdata, so I thought it is some different technique. – Indu Velayutham Feb 25 '18 at 18:26
  • 1
    Most of the time people think they need CDATA because they produce XML incorrectly - simply concatenating strings together, rather than using tools and APIs that handle escaping of characters properly. The "easy fix" is to slap CDATA around the area that could have content that would need escaping. Similarly, for receiving/parsing. When someone specifies that they require CDATA, it's a smell and makes me suspicious. – Mads Hansen Feb 25 '18 at 22:32
  • @MadsHansen Disagree. CDATA is useful if you have content which would otherwise be heavily escaped. Like XML or code fragments. CDATA makes it human-readable. – lexicore Feb 26 '18 at 08:14