Re: [xsl] Implementing JavaScript

Subject: Re: [xsl] Implementing JavaScript
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 31 Oct 2002 09:43:39 +0000
Hi Johannes,

> I'm having problems implementing JavaScript in my XSL files.

All you're doing here is creating a <script> element in the output
which has some content. XSLT doesn't know that the content of the
<script> element contains JavaScript.

You have:

>   <script language="JavaCcript">
>     <!--
>     mon=new Array
> ("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OKT","NOV","DEZ")
>     heute= new Date()
>     datstring =(heute.getDate()<10)?"0"+heute.getDate():heute.getDate
> ().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear()
>     document.write("<option>" + datstring + "</option>")
>     //-->
>   </script>

This is in an XSLT stylesheet; an XML document. The content of the
<script> element is an XML comment: <!-- ... -->. When you use a
comment in an XML document, there's no guarantee that the parser will
pass that comment through to the application. Even if it does, XSLT
processors interpret comments in the XSLT stylesheet as being comments
about the XSLT stylesheet, not content that they should put in the
result.

If you want to create a comment in the result document, then you
should use <xsl:comment>:

  <script language="JavaScript">
    <xsl:comment>
    mon=new Array
("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OKT","NOV","DEZ")
    heute= new Date()
    datstring =(heute.getDate()<10)?"0"+heute.getDate():heute.getDate
().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear()
    document.write("<option>" + datstring + "</option>")
    //</xsl:comment>
  </script>

You will then run into problems because some of the characters that
you use in the JavaScript that you're generating are significant in
XML, namely the < characters, which in XML indicate the start of a
tag. You don't want any of those characters to be interpreted as the
start of a tag, so you need to escape them. You can do that with them
individually, with:

  <script language="JavaScript">
    <xsl:comment>
    mon=new Array
("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OKT","NOV","DEZ")
    heute= new Date()
    datstring =(heute.getDate()&lt;10)?"0"+heute.getDate():heute.getDate
().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear()
    document.write("&lt;option>" + datstring + "&lt;/option>")
    //</xsl:comment>
  </script>

or you can use a CDATA section:

  <script language="JavaScript">
    <xsl:comment><![CDATA[
    mon=new Array
("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OKT","NOV","DEZ")
    heute= new Date()
    datstring =(heute.getDate()<10)?"0"+heute.getDate():heute.getDate
().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear()
    document.write("<option>" + datstring + "</option>")
    //]]></xsl:comment>
  </script>

These will both generate:

<script language="JavaScript">
   <!--
    mon=new Array
("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OKT","NOV","DEZ")
    heute= new Date()
    datstring =(heute.getDate()<10)?"0"+heute.getDate():heute.getDate
().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear()
    document.write("<option>" + datstring + "</option>")
    //--></script>

in the output, which I think is what you want.

My advice would be to put this script in an external JavaScript file
to which you refer from your HTML document. You're not using the
source document to alter what JavaScript is put in the HTML that
you're generating, so there's no need to generate it with XSLT.
  
Cheers,

Jeni

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


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


Current Thread