 |
| 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
|
|
|
|

October 7th, 2005, 05:17 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

October 7th, 2005, 05:21 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

October 7th, 2005, 07:53 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

October 7th, 2005, 10:11 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

October 10th, 2005, 05:23 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

October 10th, 2005, 05:47 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

October 10th, 2005, 07:24 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|
 |