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.

+2


source to share


7 replies


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



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

      

+7


source


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.

+3


source


Create 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 empty <script>

.

+2


source


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.

+2


source


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

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

      

+1


source


There is simply no closure </script>.

0


source


  <xsl:output method="html" omit-xml-declaration="yes" doctype-system="about:legacy-compat" encoding="utf-8"/>

      

should solve your problem

0


source







All Articles