Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDATA - FireFox and IE difference

Status
Not open for further replies.

dougalim

Programmer
Mar 1, 2007
23
US
In Firefox I can store CDATA as javascript var.innerHTML and then render it exactly as intended. IE renders it as text. The follow xml and xsl should help to understand what I'm having a problem with:

XML
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<cw>
	<content><![CDATA[<table border="1"><tr><td>Home</td></tr></table>]]></content>
</cw>

XLS
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="html" encoding="iso-8859-1" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>[/URL]
<xsl:template match="/">
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Untitled Document</title>
</head>
<body>
<div id="c"></div>
</body>

<script type="text/javascript">

var d, pagecontent;
<xsl:for-each select="cw">

	d = document.createElement("div");
	d.innerHTML = '<xsl:value-of select="content"/>'
	
	document.getElementById("c").appendChild(d);

</xsl:for-each>

</script>
</html>
</xsl:template>
</xsl:stylesheet>

Firefox renders the CDATA as a simple table with a border, IE displays it exactly as it is entered in the XML document.

I've also tried removing the <![CDATA[...]]> tags and replaced the < with &lt; and > with &gt; for the table tags and Firefox still works but IE still displays &lt;table......

Is there something I can do?
 
[0] I understand that you put the script section at the end to let the user agent properly rendered the client-side element, but I would prefer to put those lines in a function as window's onload handler. Barely exposed script lines are to my mind susceptible to mishap and of form less appealling.

[1] As to the innerHTML part, you have to "unescape" the xml built-in entities are rendering time. I've to say not quite obvious and in some sense have to work like a hack. This is how you can do it on that script section.
[tt]
<script type="text/javascript">
[blue]function x(s){
var t;
t=s.replace(/&lt;/g,"\x3c");
t=t.replace(/&gt;/g,"\x3e");
return t
}
[/blue]
[blue]function y() {[/blue]
var d, pagecontent;
<xsl:for-each select="cw">
d = document.createElement("div");
d.innerHTML = [blue]x([/blue]'<xsl:value-of select="content"/>'[blue])[/blue]
document.getElementById("c").appendChild(d);
</xsl:for-each>
[blue]}
window.onload=y;[/blue]
</script>
[/tt]
ps: don't be disturbed by my single syllable function name. You can put it as "meaningful" as you like.
 
Thank you, this has saved me a lot of work and stress. I had tried something similar but had not found any reference to \x3c or made the connection if I did see it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top