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 January 20th, 2007, 02: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);
Reply With Quote
  #2 (permalink)  
Old January 20th, 2007, 05:25 AM
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

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
Reply With Quote
  #3 (permalink)  
Old January 20th, 2007, 02: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.
Reply With Quote
  #4 (permalink)  
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.
Reply With Quote
  #5 (permalink)  
Old July 8th, 2010, 03:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,448, Level: 39
Points: 8,448, Level: 39 Points: 8,448, Level: 39 Points: 8,448, Level: 39
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,088
Thanks: 1
Thanked 181 Times in 180 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?
Reply With Quote
  #6 (permalink)  
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.
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
Executing a function on every page.. kingroon ASP.NET 2.0 Basics 5 January 14th, 2008 10: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 06:02 PM



All times are GMT -4. The time now is 01:10 PM.


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