Add a script stating XHTML namespace, it will run just fine.
This is not part of the XML standard, and as such will not be suppoted until it is (I assume this will never be supported because XML is not intended for display, but data). If you are talking about XHTML then this is a different matter.
Edit: just to clarify my answer.
XML was never intended to be a display markup like HTML, thats why XHTML was developed (HTML that conforms to XML standards). Browsers have been made to interpret XHTML in a certain way, but XML is simply raw data.
<script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[ alert('Hello'); ]]></script>
<script xmlns="http://www.w3.org/1999/xhtml" src="external.js"></script>
Works in Firefox and Opera.
I did this:
<xsl:value-of select="/label[@id='MyScript']/text()" disable-output-escaping="yes"/>
Similar to the above, but that could error because the
]]> 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>tag for the output.
// <![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:chooseblock, close then
Only steps 2, 3, 5, & 7 actually copy the script block. The rest is busywork to make it work.
Transforming a block such as...
Effectively preserved, and readable both by XML as well as a browser.
Combine these two ideas, and you'll be on your way.
©2020 All rights reserved.