views:

39

answers:

2

I have a peculiar problem with having a JavaScript for loop in XSL document. Here it goes:

I am calling a JavaScript function on the click of a CheckBox. Following is what I wanted to do in the javascript function ::

function SeelctAll()
{
     for(var cnt = 0; cnt < 100; cnt++)
     {
          //Business Logic here.
     }
}

For this I replaced < with &lt; and tried. I got an error saying "Object Expected". I then enclosed the whole function inside a <![CDATA[ section and tried. Still I got the same "Obejct Expected" error.

Any help on this would be greatly appreciable.

A: 

We'd need to see a bit more of the code here, and what process you're using to convert JavaScript in an XSL document to JavaScript in an HTML page for execution. Whilst &lt; escaping and a CDATA section are both valid ways to include out-of-band characters in an XML file, when you get to the browser side you're probably handling the page as HTML rather than native-XML, at which point the rules are different, and care is required with your HTML-generation to ensure that the output from the XSL transform is acceptable to browsers.

See if it really is the < causing the trouble rather than the elided ‘Business Logic’, by avoiding it completely. eg. replace it with something like 100>cnt.

(In any case, in general you want to keep scripting logic out of the body of an HTML page. It's better off in an external script, where you don't have to worry about the rules for embedding it in another markup language.)

bobince
A: 

Works for me:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="html" encoding="utf-8" indent="yes"/>

  <xsl:template match="/">
    <html>
      <head>
        <script type="text/javascript"><![CDATA[
          function SelectAll() {
            var x = 0;
            for(var cnt=0; cnt<100; cnt++) {
              x = cnt; // whatever
            }
            alert(x);
          }            
          ]]>
        </script>
      </head>
      <body>
        <div onclick="SelectAll()">Click!</div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

This generates:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
    function SelectAll() {
      var x = 0;
      for(var cnt=0; cnt<100; cnt++) {
        x = cnt; // whatever
      }
      alert(x);
    }
  </script>
</head>
<body>
  <div onclick="SelectAll()">Click!</div>
</body>
</html>

and it alerts the expected "99".

Tomalak
I am just having an alert statement inside the loop. BTW I have multiple xsl:templates and I do not have the <html><head><body> tags. I directly have a <table> tag.
Nagendra
@Nagendra: And... what does that mean for this answer? Your's does not work, this does. Spot the differences, draw a conclusion.
Tomalak
Thanks for the reply. I have found the differences and replaced the <xsl:output tag in my code with the one present in your code and everything started working.
Nagendra