Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 23rd, 2011, 05:17 AM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL: How to get javascript function return SELECT value in xsl

Hope anyone can help that I'm struggling to fetch JS var into xsl variable for e.g

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace">

<msxsl:script language="JavaScript" implements-prefix="js">
function getResult () {
var thisResult;
thisResult = "300";
return thisResult;
}
</msxsl:script>

<xsl:template match="/">

<xsl:variable name="JSattribute"><xsl:value-of select="js:getResult()"/></xsl:variable>

<xsl:variable name="XSLattribute">300</xsl:variable>

<xsl:choose>
<xsl:when test="string(number($JSattribute),string(number($X SLattribute))">
testAttribute is a match number
</xsl:when>
</xsl:choose>

</xsl:template>

</xsl:stylesheet>

------------

Hope anyone can help? I've tired to test many different ways to fix this error code and unable to show "testAttribute is a match number" web page all day today.

Thank you very much and look forward to read any of you crack this code?

Cheers
Oliver
Reply With Quote
  #2 (permalink)  
Old May 23rd, 2011, 07:20 AM
Friend of Wrox
Points: 6,438, Level: 34
Points: 6,438, Level: 34 Points: 6,438, Level: 34 Points: 6,438, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,196
Thanks: 0
Thanked 237 Times in 236 Posts
Default

What is it exactly that you want to achieve with
Code:
<xsl:when test="string(number($JSattribute),string(number($X  SLattribute))">
? I don't understand that snippet.
As for using Javascript extension functions with msxsl: script, that is not supported by all XSLT processors. Microsoft XSLT processors like MSXML support it, an maybe some others on Window trying to be compatible with Microsoft.
One issue I see with your XSLT code is that you have
implements-prefix="js"
yet you don't declare a namespace with that prefix. You need to do that with e.g.
Code:
xmlns:js="http://example.com/myfunctions"
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old May 23rd, 2011, 07:44 AM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default two variable

Hi Martin,

Thank you for reply which much appericate it. Sorry to confuse you. Java variable is from other data store portlet script:

1st portlet:
----------
<script type="text/javascript">
var membershipID = <xsl:value-of select="metadata/item/Membershipnumber" disable-output-escaping="yes" />;
</script>
---------

Which it will be put in this getResult () function var thisResult = "300"; e.g:

2nd portlet:
-----------

This $membershipID will be in xsl code to match other var number:

function getResult () {
var thisResult;
thisResult = $membershipID;
return thisResult;
}

* XSLattribute number for file id list

I only want to fix how to match right vraible number: JSattribute = XSLattribute.

here is renew code below include your quote "xmlns:js="http://example.com/myfunctions" that no luck.

Hope you can help me this out?

Thank you
Oliver..

---------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace">
<xsl:output method="html" encoding="utf-8"/>

<!-- Declare velocity parameters -->
<xsl:param name="context_path"/>
<xsl:param name="current_path"/>

<msxsl:script language="JavaScript" xmlns:js="http://example.com/myfunctions">
function getResult () {
var thisResult;
thisResult = $membershipID;
return thisResult;
}
</msxsl:script>

<xsl:template match="/">

<xsl:variable name="JSattribute"><xsl:value-of select="js:getResult()"/></xsl:variable>

<xsl:variable name="XSLattribute">300</xsl:variable>

<xsl:choose>
<xsl:when test="string(number($JSattribute),string(number($X SLattribute))">
testAttribute is a match number
</xsl:when>
</xsl:choose>

</xsl:template>
</xsl:stylesheet>
Reply With Quote
  #4 (permalink)  
Old May 23rd, 2011, 08:01 AM
Friend of Wrox
Points: 6,438, Level: 34
Points: 6,438, Level: 34 Points: 6,438, Level: 34 Points: 6,438, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,196
Thanks: 0
Thanked 237 Times in 236 Posts
Default

I still don't understand what you want to achieve. Do you use IE or MSXML or any XSLT processor where msxsl: script is supported? Otherwise you won't get anything working with msxsl: script, the processor is simply going to ignore it.

And you need to understand that in IE both HTML and XSLT can use Javascript functions but the execution happens at different stages, XSLT is executed first and creates a HTML document in which then, when XSLT and its scripts are completely done, other script can be used. So don't expect any script in XSLT and HTML to interact.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #5 (permalink)  
Old May 24th, 2011, 11:55 AM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, here is simple example why two variable number wont be match:

---------------------------------------------
<xsl:variable name="JSattribute">
<script type="text/javascript">document.write('300');</script>
</xsl:variable>

* HTML JSattribute Output: 300 - <xsl:copy-of select="$JSattribute" />

<xsl:variable name="XSLattribute">
300
</xsl:variable>

* HTML XSLattribute Output: 300 - <xsl:copy-of select="$XSLattribute" />

<xsl:choose>
<xsl:when test="number($JSattribute),number($XSLattribute)">
testAttribute is a match number
</xsl:when>
</xsl:choose>

*HTML Output: NA
---------------------------------------------

How to make these two variable number to match inc JS script in $JSattribute variable?

Hope this make sense?

Thank you and much appreciate!
Oliver
Reply With Quote
  #6 (permalink)  
Old May 24th, 2011, 12:52 PM
mhkay's Avatar
Wrox Author
Points: 17,773, Level: 58
Points: 17,773, Level: 58 Points: 17,773, Level: 58 Points: 17,773, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,839
Thanks: 0
Thanked 267 Times in 262 Posts
Default

The value of the XSL attribute is 300. The value of the JS attribute is an HTML
Code:
<script>
element node. For them to be equal, the HTML script element would need to be executed/evaluated while the transformation is still in progress.

It's like asking for the value 4 to be equal to the string "please add two and two".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to force the return value of xsl:function to be a string apollo13 XSLT 4 September 3rd, 2010 11:20 AM
Executing a JavaScript Function from XSL IronStar XSLT 5 July 8th, 2010 08:49 PM
Store Javascript return values in XSL variable jramesh79 XSLT 4 July 29th, 2009 07:12 AM
passing javascript variable in xsl:value-of select eruditionist XSLT 2 September 19th, 2008 06:23 PM
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 03:54 AM



All times are GMT -4. The time now is 05:31 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.