cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luca Morandini <lmorand...@ieee.org>
Subject Re: Using a Java Object from XSLT
Date Fri, 17 Mar 2006 15:27:27 GMT
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
--------------------


Mime
View raw message