Hi,

I used to work quite extensively with XSLTs about 5 years ago, using MSXML2
and 3 at the time. I remember we used <msxsl:script> quite alot to do bunch
of processing in JavaScript inside XSLT file.

Now, we are having a project that requires me to do XSLT transfomations in
servlets on Tomcat. I am using XALAN Java 2 latest version 2.7.1. I thought
similar functionality exists and indeed found about Xalan extensions. But I
was frustrated as almost nothing works.

In theollowing examples, I am not getting an error, but javascript functions
return nothing and no further output after the function call or element call
is shown.
Can someone please either help me to resolve the issue or explain why it
does not work? It would really be nice to have somethign similar to what
MSXML provides for XSLT.

To give you the better idea, I am using Tomcat 6. I put the following files
in my web application's "lib" folder:
bsf.jar, js.ja, js14.jar, serializer.jar, xalan.jar, xercesImpl.jar,
xml-apis.jar, xsltc.jar

My test code is the following:

<%@ page import="java.io.*" %>

<%
ServletContext context = pageContext.getServletContext();
File dir = new File( context.getRealPath( "" ) );

javax.xml.transform.Source xmlSource = 
        new javax.xml.transform.stream.StreamSource(new
File("C:\\Development\\Projects\\MarketingDataCoordinator\\XsltModule\\src\\TestLayout.xml"));
javax.xml.transform.Source xsltSource = 
        new javax.xml.transform.stream.StreamSource(new
File("C:\\Development\\Projects\\MarketingDataCoordinator\\XsltModule\\src\\TestXsl.xsl"));
javax.xml.transform.Result result =
                new javax.xml.transform.stream.StreamResult(out);
javax.xml.transform.TransformerFactory transFact =
                javax.xml.transform.TransformerFactory.newInstance(  );
javax.xml.transform.Transformer trans =
transFact.newTransformer(xsltSource);
trans.transform(xmlSource,result);
%>

Finally, my test XSL file is this (actually taken from from xalan download's
samples)
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
    version="1.0"   
    xmlns:xalan="http://xml.apache.org/xalan";
    xmlns:my-ext="ext2"
    extension-element-prefixes="my-ext">
    
  <!--The component and its script are in the xalan namespace and define the
implementation-->
  <xalan:component prefix="my-ext" elements="timelapse" functions="getdate">
    <xalan:script lang="javascript">
      var multiplier=1;
      // Extension element implementations always take two arguments. The
first
      // argument is the XSL Processor context; the second argument is the
element.
      function timelapse(xslProcessorContext, elem)
      {
        multiplier=parseInt(elem.getAttribute("multiplier"));
        // The element return value is placed in the result tree.
        // If you do not want a return value, return null.
        return null;
      }
      function getdate(numdays)
      {
        var d = new Date();
        d.setDate(d.getDate() + parseInt(numdays*multiplier));
        return d.toLocaleString();
      }
    </xalan:script>
  </xalan:component>
      
  <xsl:template match="deadline">
    <p>
        <my-ext:timelapse multiplier="2"/>
        We have received your enquiry and will respond by 
        <xsl:value-of select="my-ext:getdate(string(@numdays))"/>
        </p>
        Some further text after this.
  </xsl:template>

</xsl:stylesheet>


-- 
View this message in context: 
http://www.nabble.com/Xalan-J-extensions-not-working.-Is-this-correct-behavior--tp22482719p22482719.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.

Reply via email to