You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Berin Loritsch <bl...@d-haven.org> on 2006/03/17 16:12:29 UTC

Using a Java Object from XSLT

I have a Java object I am using to generate links.  It would make my 
life easier if I could use it directly in my XSLT--even though it is 
created outside of the XSLT system.  Is it as easy as passing it in as a 
parameter?  Or is there some extra leg work necessary?


Re: Using a Java Object from XSLT

Posted by Luca Morandini <lm...@ieee.org>.
Berin Loritsch wrote:
> I have a Java object I am using to generate links.  It would make my 
> life easier if I could use it directly in my XSLT--even though it is 
> created outside of the XSLT system.  Is it as easy as passing it in as a 
> parameter?  Or is there some extra leg work necessary?

It's not *that* easy, but Xalan offers various extension mechanism for 
your XSLT (sometimes you have no other choice... apart waiting for XSLT 
2.0, of course).

Here's an example using Java [1]:

The Java class:
---------------
	
Import java.util.*;

public class MyCounter {
   Hashtable counters = new Hashtable ();

   public MyCounter ()
   {}

   public void init
              ( org.apache.xalan.extensions.XSLProcessorContext context,
                org.apache.xalan.templates.ElemExtensionCall extElem )
   {
     String name = extElem.getAttribute("name");
     String value = extElem.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.extensions.XSLProcessorContext context,
                org.apache.xalan.templates.ElemExtensionCall extElem)
   {
     String name = extElem.getAttribute("name");
     Integer cval = (Integer) counters.get(name);
     int nval = (cval == null) ? 0 : (cval.intValue () + 1);
     counters.put (name, new Integer (nval));
   }
}

			
The stylesheet:
---------------

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:xalan="http://xml.apache.org/xalan"
                 xmlns:counter="MyCounter"
                 extension-element-prefixes="counter"
                 version="1.0">

   <xalan:component prefix="counter"
                    elements="init incr" functions="read">
     <xalan:script lang="javaclass" src="xalan://MyCounter"/>
   </xalan:component>

   <xsl:template match="/">
     <HTML>
       <H1>Names in alphabetical order</H1>
       <counter:init name="index" value="1"/>
       <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>

Regards,

[1] http://xml.apache.org/xalan-j/extensions.html#ex-java

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------