Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 20th, 2007, 03:10 AM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default Executing a JavaScript Function from XSL

Hi all,

I'm tyring to create an stylesheet that generates a purchase order and I am using a mix of XSL and JavaScript to control the pagination. I have an external JavaScript file (PrintedForm.js, see below) that contains a few simple functions and two variables (a row count and a maximum number of lines on the page). I increment the row count using a JavaScript function each time a <TR> is created and the row count variable in the external JavaScript file is correctly updated:


<TR></TR>
<SCRIPT LANGUAGE="JavaScript">
   incRowCount();
</SCRIPT>

I would like to test the row count value in the XSL, but I do not know how to execute the getRowCount() function and store that value in an XSL variable. I tried this:

<xsl:variable name="curRowCount" select="JavaScript:getRowCount();"/>

but I get an error saying that "JavaScript must be a name space", and I see why I got this error. I also tried this:

<xsl:variable name="curRowCount">
   <SCRIPT LANGUAGE="JavaScript">
      getRowCount();
   </SCRIPT>
</xsl:variable>

but this just returns the string "getRowCount();" and does not execute the function. Does anyone know a way to set that XSL variable using the JavaScript function or is there another way around it?

Thanks,
Tom

================================================== ========

PrintedForm.js:
// Create global variables here
var rowCount = 0;
var bodyLength = 0;

// Sets the number of rows in the body of the form
function setBodyLength(param)
{
   bodyLength = param;
}

// Initializes the row count to zero
function initRowCount()
{
   rowCount = 0;
}

function getRowCount()
{
   return rowCount;
}

function incRowCount()
{
   rowCount++;
}

function incRowCountByAmt(incBy)
{
   rowCount += incBy;
}

function addBlankRows()
{
   var diff = bodyLength - rowCount;

   document.write('<TABLE BORDER="0">');
   var i;
   for(i = 0; i < diff; i++)
   {
      document.write('<TR><TD CLASS="TDCELL"><TD></TR>');
      rowCount++;
   }
   document.write("</TABLE>");
}

function showRowCount()
{
   document.write("The row count is now "+rowCount);
 
Old January 20th, 2007, 06:25 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've got confused about the processing model (you're not the first). Writing the following in your stylesheet

   <SCRIPT LANGUAGE="JavaScript">
      getRowCount();
   </SCRIPT>

just causes a SCRIPT element to be written to the result tree. The XSLT processor doesn't treat it any differently from a <PRITSTICK> element. It's only the HTML browser, when it comes to render the generated page, that recognizes SCRIPT as something special. So you can't execute the script at transformation time and capture the result in an XSLT variable.

Some XSLT processors do allow you to call extension functions written in Javascript at transformation time. These are called from within XPath expressions in the same way as standard system functions. In MSXML for example you declare such functions using <msxsl:script>.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 20th, 2007, 03:00 PM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

Thank you for your input, Michael.
 
Old July 7th, 2010, 11:39 PM
Authorized User
 
Join Date: May 2010
Location: NY
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does anyone know if JavaScript extensions are allowed using LIBXSLT? I used them successfully with MSXML but wanted to try it with a non-Microsoft library.

Thanks.
 
Old July 8th, 2010, 03:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I can only assume that as libxslt is a C library it would allow you to write extension functions in C.

http://xmlsoft.org/XSLT/extensions.html

Quote:
Currently a libxslt module has to be compiled within the application using libxslt
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 8th, 2010, 07:49 PM
Authorized User
 
Join Date: May 2010
Location: NY
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
I can only assume that as libxslt is a C library it would allow you to write extension functions in C.

http://xmlsoft.org/XSLT/extensions.html
Thanks, Sam. I was hoping to use the much more straight-forward JavaScript :( Oh well. Thanks for your time.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a function on every page.. kingroon ASP.NET 2.0 Basics 5 January 14th, 2008 11:31 AM
Executing Javascript on Page_Load kulkarnimonica ASP.NET 2.0 Professional 2 August 10th, 2007 02:12 PM
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 02:54 AM
Executing a function mepancha SQL Server 2000 0 April 28th, 2005 10:50 AM
Executing from JavaScript C# functions see07 ASP.NET 1.x and 2.0 Application Design 1 March 1st, 2005 07:02 PM





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