[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] The old problem of Javascript, XSL and ampersand...

Subject: Re: [xsl] The old problem of Javascript, XSL and ampersand...
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 25 Jan 2005 12:26:15 GMT

    <xsl:output method="xml" omit-xml-declaration="yes" standalone="no" 

most likely you want html output not xml (if you are generating html)

    <xsl:text disable-output-escaping="yes">

You want to generate a script element node not text, so don't use
xsl:text at this point, juts have the <script..> literal result element.

to a first approximation you don't _ever_ want to use

         <script type="text/javascript" src="../IM/assets/dialog.js"></script>
         <script type="text/javascript" src="../IM/IMEStandalone.js"></script>
         <script type="text/javascript">
As mentioned above move these out of the CDATA section and out of the
xsl:text so that you get elements in the result tree.

You can use a CDATA section here to input the javascript or just
quote the < and & (it makes no difference to the output which you use)

 new ImageManager('../IM/','en');

    ImageSelector =

If you are outputting html the XSLT system knows that script is a CDATA
element so < and & must not be escaped within that element,so you wil
automatically get what you want.

If you use the xml output method, eg if you want to generate XHTML,

In your first post you said

    > As far as I can tell from the lists, there is no way to get an XSL
    > sheet to output valid javascript, the <>& chars are converted to
    > &amp; etc...
    > e.g. if(this.field && this.field.value != null) becomes
    > if(this.field &amp; &amp; this.field.value != null) which is
    > invalid JScript.

XML doesn't have CDATA elements so script is just a normal element with
PCDATA content in XHTML.

so actually if you are generating XHTML then the javascript _should_ be
XML quoted, just as for any string, the way to get a < into XML is to
write &lt;. The XML parser will see this as a < so when it passes it to
the javascript engine the javascript will see a < not a &lt;

If you have a real XHTML browser such as mozilla this is all you need.

However if you are sending the XHTML to a legacy html browser such as IE
(or sending to moz with an html mime type) then it will be treated as
html so you need to "fool" the system by using CDATA qutoing rathewr
than entity refs, just ad cdata-section-elements="script" to your
xsl:stylesheet and the system will use <![CDATA rather than &lt; which
are equivalent to an XML system but treated differently by tag soup


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

Current Thread