Monday, August 24, 2009

Including Javascript in XSL

No one tells you how to do this. If you add Javascript to HTML you end up with some left and right angle brackets and ampersands in your javascript, which you can't have in XML, before it is transformed into HTML. The thing about Xalan is that it first interprets all entity references to &amp; and &lt; before it transforms it and then puts them back again afterwards. You can disable the afterwards transformation by enclosing the script in a <xsl:text disable-output-escaping="yes"> ... </xsl:text> pair of tags. But you have to encode your angle brackets and ampersands as entities too, otherwise you'll get a syntax error. So &amp; gets transformed into & and &lt; into < during the input phase, then you get a literal angle bracket etc in your transformed Javascript. Cool.

That took me an hour and a half to work out again (I forgot how to do it) so I thought I would write it down this time.

No comments:

Post a Comment