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 October 7th, 2005, 05:17 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem calling static Java functions from XSLT

Hi all,

I'm having a spot of bother trying to get my XSLT to call external java functions. I am needing the equivalent of Math.pow(a,b). I have declared a java namespace on the stylesheet, as such:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:java="c://projects/blah/classes/net/blah/transforms"
                exclude-result-prefixes="java">

where my function is a static function in a class called 'Functions' which is in the 'transforms' package. When I try to call it using:

<xsl:value-of select="java:Functions.power(@n)"/>

I get a ClassNotFoundException: transforms/Functions

If I change the namespace path to be /transforms/Functions and call it with 'select="java:power(@n)"' then I get

java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xpath.objects.XNodeSet.power([ExpressionContext,] ).

I am sure I am just missing something obvious, if anyone can point out what I am doing wrong I would be very grateful, thanks!


 
Old October 7th, 2005, 05:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The way extension functions work depend on which XSLT processor you are using, so that's the first thing you need to tell us.

From the error message, it looks as if you're using Xalan, in which case it might be best to ask on a Xalan list.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 7th, 2005, 07:53 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, apologies for the oversight. Yes I am using the Xalan processor, although to be honest I don't really care which I have to use, as this is the only part of a large project that is using XSLT, so I am free to use whichever gets the job done. Would you be able to help with any other transformer?


 
Old October 7th, 2005, 10:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can certainly give you help if you decide to use Saxon.

For info on writing extension functions with Saxon, see http://www.saxonica.com/documentatio...functions.html

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 10th, 2005, 05:23 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the link. I was interested to note that I could call java functions easily using Saxon, as like I said above it's only Math.pow() I need at this point. So now I have

<xsl:value-of select="math:pow($currentAge,$startAge)" xmlns:math="java:java.lang.Math"/>

But of course this fails as I have not declared the java namespace yet. I am at a loss as to what this should be declared as though?

Thanks again.


 
Old October 10th, 2005, 05:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your code looks correct to me: if $currentAge and $startAge are doubles I would expect it to work. How is it failing, and which Saxon version are you using?

(There's no "java" namespace here. The prefix "java:" is being used as part of the namespace URI - technically, the scheme name of the URI).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 10th, 2005, 07:24 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Heh, I'm an idiot. The error was a leftover 'exclude-result-prefixes="java"' statement in the header, from when I was trying to get it to work with Xalan. :)

Many thanks for all your help!







Similar Threads
Thread Thread Starter Forum Replies Last Post
Help calling and using a functions in visual c++ 6 method Visual C++ 0 July 8th, 2006 05:20 PM
Calling functions ejmichaud Access VBA 2 July 16th, 2004 10:03 AM
calling functions with conditions Toka1 Javascript How-To 2 February 5th, 2004 09:29 AM
Calling functions in Windows? jacob C# 0 November 5th, 2003 03:57 PM
Calling subs & Functions Zooker Beginning VB 6 4 August 20th, 2003 07:09 PM





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