Re: [xsl] <![CDATA[ in Sablotron Parser

Subject: Re: [xsl] <![CDATA[ in Sablotron Parser
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Feb 2001 10:10:28 +0000
Hi Tim,

> In the header of the HTML files which I am creating with XSL I want
> to use javascript in the final comment tags.

I'm a bit confused by what you're after here, but it might just be
that you're trying to cater for really old browsers or something? You
seem to be thinking that you need <-- or <;-- to start a comment in
HTML - you actually need <!--.  Also, I don't think you need to wrap
Javascript in comments if it's within a script element - most browsers
understand script elements nowadays.

Anyway, the main thing is that you haven't yet grasped what CDATA
sections are all about.  The *only reason* for CDATA sections is that
they stop you from having to escape all your less-than signs and
ampersands.

  <![CDATA[< and & signs]]>

is *exactly* the same as:

  &lt; and &amp; signs

And the XSLT processor cannot tell the difference between the two.

[Note: in XHTML the content of script elements is wrapped in CDATA
sections so that they're easier to read.  You can make this happen
automatically in your output with:

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

Now, you can usually create comments in XSLT using:

  <xsl:comment>comments here</xsl:comment>

so what I'd recommend is that you use:

<script type="text/javascript" language="javascript">
   <xsl:comment>comments here</xsl:comment>
   <![CDATA[
      function someJavascript() {
         if (something) {
            thenDo.this();
         }
      }
   ]]>
</script>

Although I'd personally get rid of the CDATA section unless I had a
lot of < and & signs in my Javascript.

If you want to wrap the content above in a comment, then again you can
just use the xsl:comment element to do it:

<script type="text/javascript" language="javascript">
   <xsl:comment>comments here</xsl:comment>
   <xsl:comment>
   <![CDATA[
      function someJavascript() {
         if (something) {
            thenDo.this();
         }
      }
   ]]>
   </xsl:comment>
</script>

But this will only work if you don't want to generate any of the
Javascript using XSLT (and if you don't want to generate the
Javascript in XSLT, then why not put it in another file and just use
the src attribute to refer to it).

So, as a last resort, if you absolutely *have* to wrap the content of
the script element in a comment and you need to generate some of the
script using XSLT, then you could use disable-output-escaping to wrap
it in a comment in the output:

<script type="text/javascript" language="javascript">
   <xsl:comment>comments here</xsl:comment>
   <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
   <![CDATA[
      function someJavascript() {
         if (something) {
            thenDo.this();
            ]]><xsl:value-of select="$myjavascript" /><![CDATA[
         }
      }
   ]]>
   <xsl:text disable-output-escaping="yes">--></xsl:text>
</script>

I hope that helps,

Jeni

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



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread