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 September 7th, 2008, 10:34 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default how to pass javascript variable to xsl

I am having problem passing the javascript variable to XSL. I am not sure exactly how to accomplish it or if it can be accomplished. my JS has a function called getID which gets me id a1234 which I am using to print out the label John. Can someone show me how I can do this.

here is my XSL and XML.

<xsl:for-each select="//child[@id ='a1234']">


<br/>
<xsl:value-of select="label"/>

</xsl:for-each>






<parent id="a5678">
    <child id="a1234">
       <label key="">John</label>
    </child>
    <child id="a1235">
       <label key="">Charles</label>
    </child >
</parent>


 
Old September 8th, 2008, 01:14 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

try this:

<?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="JScript" implements-prefix="user">
   your code and function getID() here
</msxsl:script>

<xsl:template match="/">
   <xsl:value-of select="user:getID(.)"/>
</xsl:template>

</xsl:stylesheet>


 
Old September 8th, 2008, 03:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you're calling your XSLT transformation from Javascript and it's the calling program that computes the ID, then declare a stylesheet parameter <xsl:param name="id"/> and refer to it as select="//child[@id=$id]"; you can set the parameter from the calling Javascript code using a method in the API defined by your chosen XSLT processor, typically something like transformer.addParameter("id", "a1234").

Alternatively, if your XSLT processor supports it, you can call Javascript extension functions. In Microsoft's MSXML processor (and also in their .NET processor) you can write these within an <msxsl:script> element as shown in another answer to your post.

On the other hand, you might be thinking in terms of Javascript within the HTML page activated by the user clicking a button or similar. If so, you've got things wrong: the XSLT transformation can never respond directly to user interface events.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old September 8th, 2008, 08:26 AM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

Is there any way to get this without having to use msxsl? I basically want to just try getting the id using the javascript I have for example. this is my JS in CDATA

function getID(){
var id="a1234";
return id;
}

so I already have it defined I just want to know if there is a way for me to simply add the getID inside my XSLT like this to get the label according to the id

<xsl:for-each select="//child[@id='getID()']">
<br/>
<xsl:value-of select="label"/>

</xsl:for-each>


 
Old September 8th, 2008, 08:37 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Calling code in other languages (known as extension functions) depends entirely on the XSLT implementation you are using. Some processors support calls to Javascript, some don't; and those that do might do it in different incompatible ways.

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
passing javascript variable in xsl:value-of select eruditionist XSLT 2 September 19th, 2008 05:23 PM
How to pass javascript variable to server side Andraw Classic ASP Basics 17 January 22nd, 2007 01:05 PM
How To Pass Javascript variable to vbscript pbcatan Classic ASP Databases 1 December 18th, 2006 08:42 AM
Pass XSL variable back to asp.net page ayamas XSLT 8 September 14th, 2006 06:07 AM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM





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