xsl-list
[Top] [All Lists]

RE: How can I put some javascript into an xslt page

2003-06-17 09:44:08
[Lionel Crine]

I'm using xalan as salt parser.

Interesting.  I use it as a pepper parser, myself :-)


Here is what I tried but the value nbuser is not found.


<xsl:template match="users">
<xsl:variable name="nbuser">hello</xsl:variable>
  <script language="javascript">
    function lionel() {   document.write(string($nbuser)); }
  </script>
  <script language="javascript">lionel();</script>

...

Have you examined the output to see if it is what you intended?  Your
example will produce the following javascript code:

<script language="javascript">
     function lionel() {   document.write($nbuser); }
 </script>

Your javascript interpreter will not understand $nbuser.  If you want
the xslt processor to do anything you have to give it an xslt
instruction.  $nbuser is not an xslt instruction by itself.  Probably
you want this:

function lionel() {   document.write(string(<xsl:value-of
select='$nbuser'/>)); }

As a minor point, there is no reason to use a second script element for
the second command.  Just put them both in a single script element.

As a point of javascript, you do not want to use string()  If you
thought that you needed it for the xslt side of the house, xsl:value-of
will produce a string for you anyway.

As a point of xslt, you can declare your variable like this instead -

<xsl:variable name='nbuser' select='"hello"'/>

This is slightly preferable because it really does make the variable to
contain a string.  The other way it contains a Result Tree Fragment that
ha a text node, which then gets converted to a string. Notice the
quotation marks around "hello" in shorter form.  If you omit them, the
variable will contain a nodeset of elements corresponding to the XPATH
expression hello.  It will be an empty node set because you have no
elements named hello.  So make sure to use quotes when you want a string
value within an xslt attribute.

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>