Re: Xalan-J extensions not working. Is this correct behavior?

Mukul Gandhi <[email protected]>
Newsgroups gmane.text.xml.xalan.java.user
Message-ID <[email protected]>
This works fine with me.

I ran the JavaScript extension specified at,
http://xml.apache.org/xalan-j/extensions.html#ex-basic which works
fine with me.

I downloaded the following additional components,

bsf.jar from http://jakarta.apache.org/bsf/index.html

js.jar from http://www.mozilla.org/rhino/

and placed these in the system CLASSPATH.

please note that, the sample on Xalan site specifies:

<xsl:template match="deadline">

& @numdays

which means, the sample stylesheet is expecting an XML file. something like:

<deadline numdays="5" ...

I modified the sample to something like,

<xsl:template match="/">
   <p><my-ext:timelapse multiplier="2"/>We have logged your enquiry and will
      respond by <xsl:value-of select="my-ext:getdate('5')"/>.</p>
</xsl:template>

and it produces output:

<?xml version="1.0" encoding="UTF-8"?><p>We have logged your enquiry and will
      respond by March 23, 2009 12:51:11 PM IST.</p>


On Thu, Mar 12, 2009 at 11:59 PM, Bohdan <[email protected]> wrote:
>
> 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>



-- 
Regards,
Mukul Gandhi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.