5

I am using XSLT Transformation and need to put some data in CDATA section and that value is present in a variable.

Query: How to access variable in CDATA ? Sample Given Below:

<xsl:attribute name ="attributeName">
<![CDATA[ 
  I need to access some variable here like
   *<xsl:value-of select ="$AnyVarible"/>* 
 ]]>
</xsl:attribute>

How can I use varibale in CDATA ? Note: I can not use --> &lt;![CDATA[<xsl:value-of select ="$AnyVarible"/>]]&gt; Thanks in advance.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Amit
  • 3,358
  • 9
  • 34
  • 48
  • 1
    @Amit: There is no CDATA in attribute values. – Tomalak Feb 15 '10 at 17:02
  • 1
    CDATA sections are a way to simplify text editing--they let you use unescaped characters where you'd otherwise need entity references. `

    call AT&T

    ` and `

    call <![CDATA[AT&T]]>

    ` are semantically equivalent. CDATA sections may not appear in attribute values. (In SGML, you can declare an attribute's type to be CDATA, which tells the processor to treat markup in it as plain text; XML does not have this ability). You may wish to clarify what you are trying to achieve with your CDATA-in-attribute question.
    – iter Feb 15 '10 at 21:38

3 Answers3

7

I got the solution for this...FYI for everyone...

<xsl:text
disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:value-of select ="$AnyVarible"/>
<xsl:text
disable-output-escaping="yes">]]&gt;</xsl:text>
Lucero
  • 59,176
  • 9
  • 122
  • 152
Amit
  • 3,358
  • 9
  • 34
  • 48
  • 1
    The problem with this approach is that you may end up with invalid XML. Also, some XSLT processors (for example the one built into Mozilla Firefox) will not respect the `disable-output-escaping` attribute. – Lucero Feb 15 '10 at 13:00
  • @Lucero: *How* can you end up with invalid XML? Unless the variable contains `]]>` of course. – Tomalak Feb 15 '10 at 16:57
  • @Tomalak, in this case exactly with the `]]>` case. However, my comment was geared more towards the use of `disable-output-escaping` in general. – Lucero Feb 16 '10 at 10:58
  • @Amit, Thank you for answering the gist of the question instead of picking it apart. – toddmo Nov 20 '12 at 23:03
  • One should be aware that this snippet only produces valid XML if round-tripped through the string representation. If one's XSLT processor returns an XML DOM tree as the result (like, say Python's `lxml.etree` does) the XML element this snippet produces will contain a text node containing `<![CDATA[`, a set of "real" XML elements produced by `` and a trailing text node containing `]]>`. So if one would expect a single XML element with a single text node containing what's wrapped with CDATA, this expectation will fail. – kostix Mar 22 '16 at 15:09
3

CDATA is just text like any other element contents...

But using the xsl:output element you should be able to specify which elements are to be written as CDATA with the cdata-section-elements attribute.

EDIT:

Now that there is a valid sample, I guess you mean this:

<xsl:attribute name ="attributeName">
<![CDATA[ 
   I need to access some variable here like
   *]]><xsl:value-of select ="$AnyVarible"/><![CDATA[* 
]]>
</xsl:attribute>
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • yes... I need to access variable from CDATA <![CDATA[ ]]> – Amit Feb 15 '10 at 11:03
  • The `CDATA` cannot contain tags. Therefore you **must** end the `CDATA` section, add the tag, and start another `CDATA` section. – Lucero Feb 15 '10 at 12:58
  • @Amit: as per the spec a XML processor must handle CDATA sections the same way they treat simple text. So if some tool only accepts the data inside CDATA, then that tool is broken. – Joachim Sauer Feb 15 '10 at 13:00
  • To make any kind of sense, you need to declare `disable-output-escaping="yes"` in a CDATA section. Also there is no CDATA in an attribute value, AFAIK. – Tomalak Feb 15 '10 at 16:52
3

If you want to include CDATA sections in your output, you should use the cdata-section-elements atribute of xsl:output. This is a list of element names. Any such elements will have their text content wrapped in CDATA.

<xsl:output cdata-section-elements ="foo" />

<foo>
    <xsl:value-of select="$bar' />
</foo>
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77