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

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

@Paul: Have you used this suggestion and removed the superflous xsl:text element?
@AnthonyWJones yes. Still same error. I'll make a simplified example and try it again. Perhaps something else is triggering the problem.
1

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

4 Comments

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) { .... :-)
.Net seems to be the odd one out. libxslt, Saxon, Xalan-J and Sablotron spit out "&&".
'odd one out' or 'feature' or 'bug'?
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.
0

For Information I succeed to:

  • import external javascript in the HEAD part
  • define my own javascript in the body

For a reason I still ignore, the xsl:text is needed otherwise it is impossible to import an external javascript.

        <html>
        <xsl:text  disable-output-escaping="yes">
        <![CDATA[
        <head>
            <title>Products - Eshop</title>
            <link rel="stylesheet" type="text/css" href="./catalog.css"/>
            <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
            <script language="javascript" type="text/javascript" src="./jquery.condense.js"></script>
        </head>
        ]]>
        </xsl:text>   
        <body>
            <script type="text/javascript"  language="javascript">  
                <![CDATA[
                $(document).ready(function(){
                
                     $('.condensed').condense({    
                        moreSpeed: 'fast',
                        lessSpeed: 'slow',
                        moreText: 'show more',
                        lessText: 'show less',
                        ellipsis: " [more...]",
                        condensedLength: 40
                });                                     
                });
                ]]>
            </script>
            
            <h3>List of Accessories</h3>

                            ... AND SO ON ...  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.