Re: [xsl] Can't output CDATA section

Subject: Re: [xsl] Can't output CDATA section
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 7 Jul 2004 15:49:43 +0100
Hi Robert,

> How can I get the content of the CDATA section of this node?
>
> <option3link><![CDATA[javascript:objDialogBox.dlgReset();moduleFocus=old
> ModuleFocus;]]></option3link>
>
> The use of
>
> <xsl:value-of select="option3link"/>
> or
> <xsl:copy-of select="option3link/."/>
>
> doesn't work?

You don't say, but I suspect that you want the output to look like:

  <output><![CDATA[javascript:objDialogBox.dlgReset();moduleFocus=oldModuleFocus;]]></output>

and are frustrated because you're instead getting:

  <output>javascript:objDialogBox.dlgReset();moduleFocus=oldModuleFocus;</output>

The reason you're getting this output is that XSLT doesn't keep track
of which text appears within a CDATA section and which text doesn't.
As far as XSLT is concerned, your <option3link> element above is
*exactly* the same as:

  <option3link>javascript:objDialogBox.dlgReset();moduleFocus=old
  ModuleFocus;</option3link>

In both cases, you have an <option3link> element which contains a text
node whose value is "javascript:objDialogBox.dlgReset();moduleFocus=old
ModuleFocus;".

Similarly, the result tree that you generate has elements, attributes
and text nodes in it -- there aren't any "CDATA nodes".

But you can get XSLT to output an element's text nodes in CDATA
sections. To do this, use the <xsl:output> element at the top level of
the stylesheet and set the cdata-section-elements attribute to hold
the name(s) of the element(s) that should contain CDATA sections in
the output. For example:

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

means that the result of:

  <output>
    <xsl:value-of select="option3link" />
  </output>

will be:

  <output><![CDATA[javascript:objDialogBox.dlgReset();moduleFocus=old
  ModuleFocus;]]></output>

Of course you can name the element in which the CDATA section appears
anything you like -- it doesn't have to be called 'output' -- just
make sure that that's the name you list in the cdata-section-elements
attribute.
  
You might alternatively be able to use disable-output-escaping, but
I'd avoid that method if you can.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


Current Thread