Mon 3 Nov 2008
Output Correct JavaScript Elements for XHTML using XSLT
Posted by admin under html , javascript , programming , xslt[2] Comments
Say you create XHTML from XML using XSLT transformation (like me from Docbook to XHTML chunks using Docbook XSLT Stylesheets ) and you want to include some Javascript in the XHTML output.
If you serve your XHTML pages with mime-type: application/xhtml+xml rather then text/html , you will have to take care of xml-relevant characters within the script code (see Javascript and XHTML for a short explanation):
<script type="text/javascript"> /* <![CDATA[ */ // content of your Javascript goes here /* ]]> */ or <script type="text/javascript"> // <![CDATA[ content of your Javascript goes here //]]> </script>
Generating this output with xslt can be a little bit tricky. My solution (probably better exist) uses a trick shown in http://www.w3.org/TR/xslt#section-XML-Output-Method:
... xslt code ... <script type="text/javascript"> <xsl:text disable-output-escaping="yes"> <![CDATA[//<![]]><![CDATA[CDATA[]]> //javascript <![CDATA[//]]><![CDATA[]]]]><![CDATA[>]]> </xsl:text> </script> ... xslt code
November 25th, 2009 at 6:17 pm
This is exactly what I have been looking for!
June 13th, 2010 at 2:16 am
Excellent, helped me a lot. But just a detail … don’t forget to put the javascript in a CDATA element.
… xslt code …
<![CDATA[//
]]>
… xslt code