Hi, Morten --

Sorry it's taken me so long to respond.  The short answer is that it is
most likely that your Translate class is not on your classpath.  I tried
your example and, after changing the "replace" call in the stylesheet to
"replace2", it worked just fine.  Be sure that you know what your
classpath is.  Are you executing this from the command line or from a
servlet or EJB or something like that?

If you have any questions regarding this, look at Shane's
org.apache.xalan.xslt.EnvironmentCheck class and follow the directions
in the javadoc there.

You didn't include a stack trace so I can't be sure but if you see the
class org.apache.xalan.extensions.ExtensionHandlerJavaPackage near the
top of the trace, this indicates that java couldn't find your
dk.bording.axt.tc.xsl.Translate class and is instead trying to treat it
as a package.

HTH,
Gary

> -----Original Message-----
> From: Morten Grue Nielsen [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, December 18, 2001 4:43 AM
> To: '[EMAIL PROTECTED]'
> Subject: XALAN 2 - Using Java Extension Functions
> 
> 
> Hi
> 
> I have made a Translate class, in which a replace method is 
> found. This method replace words, with other in a string. I 
> have a problem trying to get to this method by using xslt 
> extension functions with xalan j_2_2_D14. Instead of using 
> the replace method in my Translate class, it uses the replace 
> method in the java.lang.String class. Can you help me ??
> 
> ---------- The xslt script : -----------
> 
> xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>  xmlns:lxslt="http://xml.apache.org/xslt";
>  xmlns:bdf="dk.bording.axt.tc.xsl.Translate"
>  extension-element-prefixes="bdf"
>  version="1.0">
> 
>  <lxslt:component prefix="bdf" functions="replace">
>   <lxslt:script lang="javaclass" 
> src="xalan://dk.bording.axt.tc.xsl.Translate"/>
>  </lxslt:component>
> 
>  <xsl:output indent="yes" method="xml" encoding="iso-8859-1" 
> cdata-section-elements="VALUE NAME KEY SOURCE pling"/>
> 
>  <xsl:template match="/">
>   <xsl:apply-templates select="text()"/>
>   <document>
>    <pling>
>     <xsl:for-each select="function/test">
>      <xsl:value-of select="bdf:replace(string(text()),'asd','abc')"/>
>     </xsl:for-each>
>    </pling>
>   </document>
>  </xsl:template>
> </xsl:stylesheet>
> 
> 
> ------- Java file --------
> 
> package dk.bording.axt.tc.xsl;
> 
> import org.w3c.dom.*;
> import org.apache.xalan.extensions.*;
> import org.w3c.dom.traversal.*;
> import org.apache.xalan.templates.*;
> 
> public class Translate
> {
>       static StringBuffer sb;
>       static int i;
>       static String s1;
> 
>       public static String replace(XSLProcessorContext 
> processorContext, ElemExtensionCall extElem) {
>       return "hest";
>       }
> 
>       public static String replace(NodeList nl, String s2, 
> String s3) {
>               for (int i = nl.getLength() - 1; i >= 0; i--) {
>                       s1 = (String)nl.item(i).getNodeValue();
>                       s1 = (String)rep(s1, "%pl%", "'");
>                       s2 = (String)rep(s2, "%pl%", "'");
>                       s3 = (String)rep(s3, "%pl%", "'");
>               }
>               return rep(s1,s2,s3);
>       }
> 
>       public static String replace(ExpressionContext ec, 
> String s2, String
> s3) {
>               NodeIterator ni = ec.getContextNodes();
>               boolean more = true;
>               while (more) {
>                       Node n = ni.nextNode();
>                       if (n != null) {
>                               more = true;
>                       } else {
>                               more = false;
>                       }
>                       s1 = (String)n.getNodeValue();
>                       s1 = (String)rep(s1, "%pl%", "'");
>                       s2 = (String)rep(s2, "%pl%", "'");
>                       s3 = (String)rep(s3, "%pl%", "'");
>               }
>               return rep(s1,s2,s3);
>       }
> 
>       public static String replace2(String s1, String s2, String s3) {
>               s1 = (String)rep(s1, "%pl%", "'");
>               s2 = (String)rep(s2, "%pl%", "'");
>               s3 = (String)rep(s3, "%pl%", "'");
>               return rep(s1,s2,s3);
>       }
> 
>       private static String rep(String s1, String s2, String s3) {
>               int l = s1.length();
>               for (int i2=0;i2<l;i2++) {
>                       char rp = s2.charAt(0);
>                       char fc = s1.charAt(i2);
>                       if (rp == fc) {
>                               String s = s1.substring(i2, i2 +
> s2.length());
>                               if (s.equals(s2)) {
>                                       StringBuffer sb = new
> StringBuffer(s1);
>                                       sb.delete(i2, i2 + s2.length());
>                                       l = l - s2.length();
>                                       sb.insert(i2, s3);
>                                       l = l + s3.length();
>                                       s1 = sb.toString();
>                                       for (int 
> i3=0;i3<s3.length();i3++) {
>                                               i2++;
>                                       }
>                               }
>                       }
>               }
>               return s1;
>       }
> }
> 

Reply via email to