The task seems to be pretty easy: how to include a Javascript file in xml-document so that at least Opera and Firefox could actually parse it and execute the code?
Similar to the above, but that could error because the <![CDATA[
and ]]>
portions are not valid code. If you're putting it into an XSL script, you can just as well put a JS comment mark before these beginning and ending elements.
I also have used the xsl:text
element to output the <![CDATA[
portion. This portion may be a bit of cheat, but it results in well-formed XML. An example from within an xsl:choose
block might be...
...
<xsl:when test='name()="script"'>
<script>
<xsl:for-each select='@*'><xsl:copy-of select='.' /></xsl:for-each>
<xsl:text disable-output-escaping='yes'>
// <![CDATA[
</xsl:text>
<xsl:copy-of select='./text()' />
<xsl:text disable-output-escaping='yes'>
//]]>
</xsl:text>
</script>
</xsl:when>
...
Walking through the pieces...
<script>
element.<script>
tag for the output.xsl:for-each
line.// <![CDATA[
. The //
renders the rest of the line as a comment and thus prevents a JS error.<script>
tag. NOTE: You must preseve the new-line (either as above or some other way) so that the commented out line before it does not end up on the same line as this one. Obviously, if it does, it will comment out this line as well. Preserving the one after is not essential, but is keeps the aesthetics of the two matching CDATA tags.// ]]>
. This ends the CDATA block, and again, the CDATA marking is ignored by the browser when reading the JS.</script>
tag, of course.xsl:choose
block, close then xsl:when
.Only steps 2, 3, 5, & 7 actually copy the script block. The rest is busywork to make it work.
Transforming a block such as...
...
<script type='javascript'>alert('Hello World!');</script>
...
Then becomes,
...
<script type='javascript'>
// <![CDATA[
alert('Hello World!');
// ]]>
</script>
Effectively preserved, and readable both by XML as well as a browser.