views:

291

answers:

6

I have a C# application that generates an html document from transforming an xml file with an xsl file. In my xsl template I reference an external javascript file like this:

<script language="javascript" type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" ></script>

after the transformation the previous line is being translated to:

<script language="javascript" type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" />

For Firefox and Chrome this is no problem however IE throws an 'object not found' error and does not work. Any suggestions for getting IE to like this syntax? Or is there something I need to do in my xsl (or the C# XslCompiledTransform class) to preserve the syntax?

Solution: By placing <![CDATA[ <!-- Some Comment --> ]]> between the script tags the parser doesn't attempt to shorten the ending tag.

+2  A: 

Just Missing the closing </script>.

corymathews
Correct. IE down not allow <script ... />. You have to use <script></script>.
Nissan Fan
I have the syntax that way in my XSL template however after translation using the XslCompiledTransform object in C# the <script></script> is changed to <script />
jwarzech
In XML, a self-closing element is identical to an empty full element, so XSLT is free to serialize it either way.
Pavel Minaev
+2  A: 

Try putting an empty CDATA section inside. This should force the parser to not mess with your script tags.

<script language="javascript" type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" ><![CDATA[ ]]></script>
Chetan Sastry
That didn't work however by putting a comment in the CDATA section it forced the parser not to remove the ending script tag. Thanks for the suggestion!
jwarzech
+1  A: 

Generate an XML comment inside <script>:

<script type="text/javascript" src="..." ><xsl:comment/></script>

The output will be:

<script type="text/javascript" src="..."><!-- --></script>

which is semantically equivalent to an empty <script>.

Pavel Minaev
A: 
bobince
It won't actually help, because `method="html"` does not in any way affects self-closing element behavior for `XslCompiledTransform` (I'm not sure if it actually does anything else).
Pavel Minaev
... in fact, I don't see anything in XSLT 1.0 spec that would require `<script>` element to _not_ be written as self-closing if its empty, even with `method="html"` (see http://www.w3.org/TR/xslt#section-HTML-Output-Method).
Pavel Minaev
Not escaping `<` is actually perfectly correct behavior for `<script>` and `<style>` elements, because that's how HTML spec itself defines those elements - their content type is CDATA in DTD (http://www.w3.org/TR/REC-html40/sgml/dtd.html#Script), and also 6.14 says: "Please note that script data that is element content may not contain character references".
Pavel Minaev
Yes, in CDATA elements. However XSLT also does it in attribute values. God knows why, maybe some ancient long-forgotten broken browser not supporting <?
bobince
+1  A: 

Actually, bobince is right. If you use...

<xsl:output method="html"/>

... you can get the right output for XslCompiledTransform, but you have to use its OutputSettings with the XmlWriter you use as output object:

XslCompiledTransform xslt = new XslCompiledTransform(true);

xslt.Load("stylesheetFile.xsl");

XmlWriter outputWriter = XmlWriter.Create("outputfile.html", xslt.OutputSettings);

xslt.Transform(input, null, outputWriter);

This way, the method="html" works, so script, textarea et al keep their closing tags.

Ricardo Nolde
A: 

had the same prob. right now, this is my solution:

<xsl:text disable-output-escaping="yes">
  <![CDATA[<script type="text/javascript" src="" ></script>]]>
</xsl:text>
snzro