views:

126

answers:

2

I am transforming xml to html using xslt in .Net 1.1. One part contains a javascript section where 2 vars are ANDed (&&). The transform throws an unknown entity error. What can I do? I have tried 'CDATA' and 'disable-output-escaping' but without success. If I write && then the output is also '&&'.

Here is my code. (trimmed for clarity)

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="html" standalone="yes" indent="yes" cdata-section-elements="script style"/>

    <xsl:template match="/menus">
<html>
<xsl:text disable-output-escaping="yes">
    <head>
        <title>Portal</title>
        <script type="text/javascript">
//
// Show hide language block
//
function lang(s) {
    var elD = document.getElementById('german');
    var elE = document.getElementById('english');
    if(elD && elE) {    /// <<---- error occurs here
        elD.style.display = s == 'german' ? 'block': 'none';
        elE.style.display = s == 'german' ? 'none': 'block';
    }
}
        </script>
</head>
</xsl:text> 
<body>
    <h2>Das Portal ist vorübergehend unerreichbar / The Portal is temporarily unavailable</h2>

    <div><a href="#" onclick="lang('german')">deutsch</a> | <a href="#"  onclick="lang('english')">English</a>
    </div>

    <div id="german">
        <xsl:apply-templates select="//menu[@lang='de']"/>
    </div>

    <div id="english">
        <xsl:apply-templates select="//menu[@lang='en']"/>
    </div>
</body>
</html> 
</xsl:template>
A: 

Try it as character entities: if(elD &amp;&amp; elE) and move the <xsl:text> tags inside <script>.

outis
did that. Just got carried straight through to the output so JS threw an error instead. I'm currently using the pragmatic approach -- if(elD) if(elE) { .... :-)
paul
outis
'odd one out' or 'feature' or 'bug'?
paul
It wouldn't be the first time an MS technology differed from the standard, but I haven't looked at them closely enough to tell.
outis
+1  A: 

You could try putting your script in a CDATA section. AFAIK a CDATA section in your XSLT file will not translate to a CDATA section in your output file.

<script language="JavaScript">
  <![CDATA[
// 
// Show hide language block 
// 
function lang(s) { 
    var elD = document.getElementById('german'); 
    var elE = document.getElementById('english'); 
    if(elD && elE) {    /// <<---- error occurs here 
        elD.style.display = s == 'german' ? 'block': 'none'; 
        elE.style.display = s == 'german' ? 'none': 'block'; 
    } 
} 
  ]]>
</script>
Joe
tried that too. The transformer still throws an error
paul
@Paul: Have you used this suggestion and removed the superflous xsl:text element?
AnthonyWJones
@AnthonyWJones yes. Still same error. I'll make a simplified example and try it again. Perhaps something else is triggering the problem.
paul