[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: xslt extensions error : 'Call to extension functio
Here is the code that I am using. This is the basic example that you get from xalan, the MyCounter.java example with slight modifications. The code is as below, MyCounter.java --------------------------------------------------------------------- package com.example.util; import java.util.Hashtable; public class MyCounter { static Hashtable counters = new Hashtable (); public void init(org.apache.xalan.xslt.XSLProcessorContext context, org.w3c.dom.Element elem) { String name = elem.getAttribute("name"); String value = elem.getAttribute("value"); int val; try { val = Integer.parseInt (value); } catch (NumberFormatException e) { e.printStackTrace (); val = 0; } counters.put (name, new Integer (val)); } public int read(String name) { Integer cval = (Integer)counters.get(name); return (cval == null) ? 0 : cval.intValue(); } public void incr(org.apache.xalan.xslt.XSLProcessorContext context, org.w3c.dom.Element elem) { String name = elem.getAttribute("name"); Integer cval = (Integer) counters.get(name); int nval = (cval == null) ? 0 : (cval.intValue () + 1); counters.put (name, new Integer (nval)); } } The xml and xsl source files are 4-numlistJava.xsl: ---------------------------------------------------------------------------- --------- <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:counter="com.example.util.MyCounter" extension-element-prefixes="counter" version="1.0"> <lxslt:component prefix="counter" elements="init incr" functions="read"> <lxslt:script lang="javaclass" src="com.example.util.MyCounter"/> </lxslt:component> <xsl:template match="/"> <HTML> <H1>Java Example</H1> <counter:init name="index" value="1"/> <p>Here are the names in alphabetical order by last name:</p> <xsl:for-each select="doc/name"> <xsl:sort select="@last"/> <xsl:sort select="@first"/> <p> <xsl:text>[</xsl:text> <xsl:value-of select="counter:read('index')"/> <xsl:text>]. </xsl:text> <xsl:value-of select="@last"/> <xsl:text>, </xsl:text> <xsl:value-of select="@first"/> </p> <counter:incr name="index"/> </xsl:for-each> </HTML> </xsl:template> </xsl:stylesheet> numlist.xml: --------------------------------------------------------------------- <?xml version="1.0"?> <doc> <name first="Sanjiva" last="Weerawarana"/> <name first="Joseph" last="Kesselman"/> <name first="Stephen" last="Auriemma"/> <name first="Igor" last="Belakovskiy"/> <name first="David" last="Marston"/> <name first="David" last="Bertoni"/> <name first="Donald" last="Leslie"/> <name first="Emily" last="Farmer"/> <name first="Myriam" last="Midy"/> <name first="Paul" last="Dick"/> <name first="Scott" last="Boag"/> <name first="Shane" last="Curcuru"/> <name first="Marcia" last="Hoffman"/> <name first="Noah" last="Mendelsohn"/> <name first="Alex" last="Morrow"/> </doc> ------------------------------------------------------------------------ Finally the code for transforming the xml and xsl to HTML is as follows: (This is a JSP page) test.jsp: ---------------------------------------------------------------------------- ------ XSLTProcessor processor = XSLTProcessorFactory.getProcessor( new XML4JLiaison4dom() ); XSLTInputSource xmlSource = new XSLTInputSource("D://numlist.xml" ); XSLTInputSource xslSource = new XSLTInputSource("D://4-numlistJava.xsl" ); XSLTResultTarget result = new XSLTResultTarget( out ); processor.process( xmlSource, xslSource, result ); The output that I get is Output: ---------------------------------------------------------- Java Example Here are the names in alphabetical order by last name: []. Auriemma, Stephen []. Belakovskiy, Igor []. Bertoni, David []. Boag, Scott []. Curcuru, Shane []. Dick, Paul []. Farmer, Emily []. Hoffman, Marcia []. Kesselman, Joseph []. Leslie, Donald []. Marston, David []. Mendelsohn, Noah []. Midy, Myriam []. Morrow, Alex []. Weerawarana, Sanjiva ---------------------------------------------------- So basically the extension function is not being called. The error message that I got is "Call to extension function failed: com.example.util.MyCounter" This is all the error that I am getting and I have no clue where the error is. Any help on this will be appreciated. Amit -----Original Message----- From: Vasu Chakkera [mailto:vasucv@xxxxxxxxxxx] Sent: Wednesday, October 02, 2002 8:41 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: xslt extensions error : 'Call to extension function failed' It is difficult to tell you what the problem is , unless u show the code.But it is very likely that you have not given the namespace declaration properly. for example, if u have a class that is called com.myexample.myclass and if u wanted to refer to it as myclass, then your <stylesheet> element should look like. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myclass = "com.myexample.myclass" extension-element-prefixes ="myclass"> and u must refer to the class method with "myclass". eg.. if u have a method called calculate() in your class, then <xsl:value-of select="myclass:calculate()"/> would make a call to the calculate() method of the class myclass. HTH Vasu > I am trying to use xslt java extensions and am having problems when >I write > >my own custom classes and try to use them. When I use the "xmlns:java" > >namespace the code runs fine. > >The error I get when using my custom class is : "Call to extension function > >failed: com.example.DateFormatter". The problem is the error doesnt give >out > >much information. Does anyone know what the possible cause'(s) of this >error > >are. Btw the code happens to run on an appserver. Thanks in advance for all > >the help. > > > >Amit _________________________________________________________________ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|