[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Implementing JavaScript
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()<10)?"0"+heute.getDate():heute.getDate ().toString()+"-"+mon[heute.getMonth()]+"-"+heute.getFullYear() document.write("<option>" + datstring + "</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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|