XSL output method to XML & JavaScript problem

J

johkar

When the output method is set to xml, even though I have CDATA around
my JavaScript, the operaters of && and < are converted to XML character
entities which causes errors in my JavaScript. I know that I could
externalize my JavaScript, but that will not be practical throughout
this application. Is there any way to get around this issue? Xalan
processor. Stripped down stylesheet below along with XHTML output.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="no" omit-xml-declaration="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
encoding="iso-8859-1" />

<xsl:template match="/">
<html>
<head>
<!-- CALL HEAD FILES -->
</head>
<body>
<script type="text/javascript">
<![CDATA[

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr;
for(i=0;a && i < a.length && (x=a) && x.oSrc;i++)x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0;
i<a.length; i++)
if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers.document);
if(!x && document.getElementById) x=document.getElementById(n); return
x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}
}
]]>
</script>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


Here is the outputted code:

<script type="text/javascript">
function MM_swapImgRestore() { //v3.0

var i,x,a=document.MM_sr;

for(i=0;a &amp;&amp; i &lt; a.length &amp;&amp; (x=a) &amp;&amp;
x.oSrc;i++)x.src=x.oSrc;

}



function MM_preloadImages() { //v3.0

var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0;
i&lt;a.length; i++)

if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}

}



function MM_findObj(n, d) { //v4.0

var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))&gt;0&amp;&amp;parent.frames.length) {

d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

if(!(x=d[n])&amp;&amp;d.all) x=d.all[n]; for
(i=0;!x&amp;&amp;i&lt;d.forms.length;i++) x=d.forms[n];

for(i=0;!x&amp;&amp;d.layers&amp;&amp;i&lt;d.layers.length;i++)
x=MM_findObj(n,d.layers.document);

if(!x &amp;&amp; document.getElementById) x=document.getElementById(n);
return x;

}



function MM_swapImage() { //v3.0

var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i&lt;(a.length-2);i+=3)

if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}

}
</script>
 
J

johkar

I think I found the solution. I put <xsl:comment> tags around the CDATA
tags and that seemed to work. I would be interested if this is the
right way to go though.
 
M

Martin Honnen

johkar said:
I think I found the solution. I put <xsl:comment> tags around the CDATA
tags and that seemed to work. I would be interested if this is the
right way to go though.

Well you will have the script code commented out then if an XML parser
parses it.

But your example code

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="no" omit-xml-declaration="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
encoding="iso-8859-1" />

<xsl:template match="/">
<html>

seems to indicate that you are not writing XHTML anyway as you are not
using the proper XHTML namespace at all e.g.
<html xmlns="http://www.w3.org/1999/xhtml">

Not sure why you think then you want output method="xml" and an XHTML
1.0 document type declared but probably you are just on the wrong path
with XHTML 1.0 use instead of HTML 4.01 for text/html. See
<http://www.hixie.ch/advocacy/xhtml>
 
J

johkar

Thank you for the reply, information and link Martin. I guess I have
read some of this before, but for the "average" web guy trying to do
what is right it sure is confusing. All the "tableless layout" CSS
gurus recommend XHTML. I validate content with both Dreamweaver and
at W3C.

One other thing, isn't the namespace "applied" if not declared?
<html xmlns="http://www.w3.org/1999/xhtml">

Thanks again
 
M

Martin Honnen

johkar said:
One other thing, isn't the namespace "applied" if not declared?
<html xmlns="http://www.w3.org/1999/xhtml">

Applied by whom? The external DTD
<http://www.w3.org/TR/xhtml1/dtds.html#dtdentry_xhtml1-strict.dtd_html>
defines
xmlns %URI; #FIXED 'http://www.w3.org/1999/xhtml'
thus if you have a validating parser reading that DTD then yes, that
attribute is then added.

Only browsers like Opera or Mozilla do not use a validating parser thus
if you want to serve them XHTML as real XML and understand that an
element in your document is an element in the XHTML namespace then your
markup has to have the namespace declaration.

The XML namespaces recommendation
<http://www.w3.org/TR/REC-xml-names/#ns-using> warns:

"This constraint may lead to operational difficulties in the case
where the namespace declaration attribute is provided, not directly in
the XML document entity, but via a default attribute declared in an
external entity. Such declarations may not be read by software which is
based on a non-validating XML processor. Many XML applications,
presumably including namespace-sensitive ones, fail to require
validating processors. For correct operation with such applications,
namespace declarations must be provided either directly or via default
attributes declared in the internal subset of the DTD."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top