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 May 23rd, 2011, 04: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
 
Old May 23rd, 2011, 06:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
 
Old May 23rd, 2011, 06: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>
 
Old May 23rd, 2011, 07:01 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
 
Old May 24th, 2011, 10: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
 
Old May 24th, 2011, 11:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





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 10:20 AM
Executing a JavaScript Function from XSL IronStar XSLT 5 July 8th, 2010 07:49 PM
Store Javascript return values in XSL variable jramesh79 XSLT 4 July 29th, 2009 06:12 AM
passing javascript variable in xsl:value-of select eruditionist XSLT 2 September 19th, 2008 05:23 PM
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 02:54 AM





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