Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 21st, 2015, 12:18 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default Problem with XSLT variable into JS

I am trying to push a XSLT variable into a JS function, but I am getting a 'Unexpected token' error from JS.

Look what I have:

Code:
<!-- it loads the test variable 'mypic2' with 'mypicname.jpg' -->
<xsl:variable name="mypic2">
   <xsl:value-of select="'mypicname'" disable-output-escaping="yes" />.jpg
</xsl:variable>

<!-- It show correctly the content of the variable mypic2 -->
My content is: <xsl:value-of select="$mypic2"/>

<!-- But IT is causing the error 'mypicname is not defined' -->
<script type="text/javascript" >
   var picurl = <xsl:value-of select="$mypic2" />;
   alert (picurl);
</script>

<!-- I tried to remove the $ but then it alerts 'undefined' -->
<script type="text/javascript" >
   var picurl = <xsl:value-of select="mypic2" />;
   alert (picurl);
</script>
What am I not seeing here?
 
Old January 21st, 2015, 12:28 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Not sure, as can't see your XML, but don't you need quotes around the path?
Code:
<script type="text/javascript" >
   var picurl = "<xsl:value-of select="$mypic2" />";
   alert (picurl);
</script>
__________________
Joe
http://joe.fawcett.name/
 
Old January 21st, 2015, 12:44 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Nope, now it says 'Unexpected token ILLEGAL'.

Tried double quotes outside and single quotes inside.
Tried single quotes outside and double quotes inside.

The error persists. That's very weird.
 
Old January 21st, 2015, 12:50 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Show the JavaScript that is created.
__________________
Joe
http://joe.fawcett.name/
 
Old January 21st, 2015, 01:08 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Look what's being rendered:

Code:
<script type="text/javascript">
                                      var picurl = "mypicname.jpg
                          ";
                                      alert (picurl);
                                    </script>
Would be the line break and the lots of spaces causing it?
 
Old January 21st, 2015, 01:14 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Yes, there are a number of solutions, this should work:
Code:
<script type="text/javascript" >
   var picurl = <xsl:text>"</xsl:text><xsl:value-of select="$mypic2" /><xsl:text>"</xsl:text>;
   alert (picurl);
</script>
or, failing that, try:
Code:
<script type="text/javascript" >
   var picurl = "<xsl:value-of select="concat('&quot;', $mypic2, '&quot;')" />";
   alert (picurl);
</script>
__________________
Joe
http://joe.fawcett.name/
 
Old January 21st, 2015, 01:27 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Why have you used
Code:
<xsl:variable name="mypic2">
   <xsl:value-of select="'mypicname'" disable-output-escaping="yes" />.jpg
</xsl:variable>
?
Simply do
Code:
<xsl:variable name="mypic2" select="'mypicname.jpg'"/>
if you want to assign a literal string.
Then to construct the Javascript string literal assigned to the variable use
Code:
var  picurl = "<xsl:value-of select="$mypic2"/>";
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 21st, 2015, 03:03 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by joefawcett View Post
Yes, there are a number of solutions, this should work:
Code:
<script type="text/javascript" >
   var picurl = <xsl:text>"</xsl:text><xsl:value-of select="$mypic2" /><xsl:text>"</xsl:text>;
   alert (picurl);
</script>
or, failing that, try:
Code:
<script type="text/javascript" >
   var picurl = "<xsl:value-of select="concat('&quot;', $mypic2, '&quot;')" />";
   alert (picurl);
</script>
Joe, unfortunately these solutions didn't work but at least it put a light over the problem and could fix it with:

Code:
var picurl = '<xsl:value-of select="normalize-space($mypic)" />';
 
Old January 21st, 2015, 03:09 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Why have you used
Code:
<xsl:variable name="mypic2">
   <xsl:value-of select="'mypicname'" disable-output-escaping="yes" />.jpg
</xsl:variable>
?
Simply do
Code:
<xsl:variable name="mypic2" select="'mypicname.jpg'"/>
if you want to assign a literal string.
Then to construct the Javascript string literal assigned to the variable use
Code:
var  picurl = "<xsl:value-of select="$mypic2"/>";
Lol, this is only a test code. Actually in the place of 'mypicname' I will have an url gathered from database and the .jpg has to be concatenated later.

 
Old January 22nd, 2015, 06:25 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The problem is in this code:

Code:
<xsl:variable name="mypic2">
   <xsl:value-of select="'mypicname'" disable-output-escaping="yes" />.jpg
</xsl:variable>
What this creates in mypic is a text node containing a carriage return, followed by some whitespace, followed by the result of the xsl:value-of instruction, followed by ".jpg" and then followed by some more whitespace and a carriage return.

As this last whitespace and carriage return is part of the 'text' at the end of the variable it is considered significant, and is output if you then do xsl:value-of this variable.

The simplest solution to this is to do this instead:

Code:
<xsl:variable name="mypic2" select="concat('mypicname', '.jpg')"/>
or ensure that the variable body contains no whitespace:

Code:
<xsl:variable name="mypic2"><xsl:value-of select="'mypicname'" />.jpg</xsl:variable>
Obviously in either example you can replace 'mypicname' string literal with your xpath to return the actual image filename.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
@include js variable smys123 JSP Basics 0 May 17th, 2011 12:17 PM
Pass a Variable from VBS to JS kriemer Javascript 7 March 8th, 2009 04:09 PM
assign XSLT variable to a wml variable arunagottimukkala XSLT 2 October 19th, 2007 05:15 AM
only do HTML/ASP when JS variable = true crmpicco Javascript How-To 1 July 19th, 2005 05:06 AM
use a JS-variable/function inside a tag cybermaarten Javascript 3 January 20th, 2005 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.