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
  • 781
  • 2
  • 14
  • 25
  • In this case, `Dear Customer` and `<![CDATA[Dear Customer]]>` are equivalent. CDATA section is useful if the data contain special characters like `<`, `>`, or `&`, but you can use it for any string. – choroba Feb 25 '18 at 10:48
  • Ok nice, but just wanted to know , if I have <![CDATA[&dear customer]] .. how my xsl should be to print this particular – Indu Velayutham Feb 25 '18 at 10:59
  • Do you actually have an ampersand in there? There is no ampersand in the question. – mzjn Feb 25 '18 at 12:16
  • Yeahhh... it is not in the question. But am asking if any special character is in the CDATA, how should I retrieve – Indu Velayutham Feb 25 '18 at 17:25
  • `&dear customer` and `<![CDATA[&dear customer]]>` are equivalent. When you obtain the text value (using XML tools and APIs), it will be the same. – Mads Hansen Feb 25 '18 at 22:45
  • Hi Indu Velayutham, is there something I should add to my answer? All the best. – jschnasse Feb 28 '18 at 20:25
  • No thanks , it’s working fine and I delivered the file☺️ – Indu Velayutham Feb 28 '18 at 21:47

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
  • 8,526
  • 6
  • 32
  • 72
  • 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