[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: RE: How can I put some javascript into an xslt page
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Tue, 17 Jun 2003 12:44:08 -0400
javascript into xslt
[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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.