Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XSL output method to XML & JavaScript problem (http://www.velocityreviews.com/forums/t169927-xsl-output-method-to-xml-and-javascript-problem.html)

johkar 09-25-2005 02:12 PM

XSL output method to XML & JavaScript problem
 
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:output 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[i]) && 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[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].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[i]))!=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[i]) &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[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}



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[i][n];

for(i=0;!x&amp;&amp;d.layers&amp;&amp;i&lt;d.layer s.length;i++)
x=MM_findObj(n,d.layers[i].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[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}

}
</script>


johkar 09-25-2005 02:30 PM

Re: XSL output method to XML & JavaScript problem
 
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.


Martin Honnen 09-25-2005 02:48 PM

Re: XSL output method to XML & JavaScript problem
 


johkar wrote:

> 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:output 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>


--

Martin Honnen
http://JavaScript.FAQTs.com/

johkar 09-25-2005 05:56 PM

Re: XSL output method to XML & JavaScript problem
 
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


Martin Honnen 09-25-2005 07:12 PM

Re: XSL output method to XML & JavaScript problem
 

johkar wrote:

> 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."



--

Martin Honnen
http://JavaScript.FAQTs.com/


All times are GMT. The time now is 02:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.