XSL parsing cuts script tag causing problems in IE
I have a C # application that generates an html document from an xml file transformation with an xsl file. In my xsl template I am referencing 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 conversion, the previous line is 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 not a problem, however IE throws an "object not found" error and does not work. Any suggestions for making IE like this syntax? Or is there something I need to do in my xsl (or C # XslCompiledTransform class) to keep the syntax safe?
Solution: . By placing <![CDATA[ <!-- Some Comment --> ]]>
between script tags, the parser does not try to shorten the end tag.
source to share
Actually, bobbinj is right. If you are using ...
<xsl:output method="html"/>
... you can get the correct output for the XslCompiledTransform, but you must use its OutputSettings with the XmlWriter you are using as the 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 is how the = "html" method works, so script, textarea, etc. keep their closing tags.
source to share
Don't save it, but if you are creating backward-compatible HTML you need to tell the XSLT processor that HTML-XHTML-compatible is what you want, not generic XML that allows closures:
<xsl:output method="xhtml"/>
Unfortunately, the xhtml output method is an XSLT 2.0 extension that .NET XslTransform does not support, so you must use the old old HTML instead:
<xsl:output method="html"/>
(and the corresponding HTML 4.01 DOCTYPE instead of XHTML.)
Putting some dummy content in <script>
might solve your immediate problem, but there might be other places where the default "xml" output method would result in incorrect markup for legacy browsers like IE.
Re: comment. Hmm ... you're right! The html output method does not produce valid HTML; "the xhtml output method does not produce XHTML compliant XHTML application C. What's more," html includes provisions such as "no escaping" <, and removing Netscape 4 screens with ancient and broken times for it ", & {. ..}, this will take your working markup and invalidate it.
So changing the output method is completely useless and the only way to create working HTML with XSLT is:
and. hack each occurrence of a non-matching self-closing tag manually (there will probably be a lot more than just this script), or
b. post process with something like HTMLTidy.
How sad and careless that this was not addressed even in XSLT 2.0.
source to share