Hello,

I'm currently working on an extension class for use in an XSL file.

The transformation I want to perform is the following:

Input fragment:
<floatlist>0 1 2 3 4 5 6 7 8</floatlist>

Output fragment:
<vec3 value="0 1 2"/>
<vec3 value="3 4 5"/>
<vec3 value="6 7 8"/>

As to my knowledge there is no way to get this working in XSLT directly (and because for the project behind it there is some more processing required than shown above anyway), I wrote an extension function to do this, and invoke it with this XSL fragment:

<xsl:for-each select="floatlist">
   <xsl:value-of value="ext:makevec3s(.)"/>
</xsl:for-each>

So the extension function is called for each <floatlist/> element, takes it's contents as a string, and creates a number of <vec3/> elements in a DocumentFragment instance.

However, although the function itself seems works fine, it appears as if Xalan simply discards the returned DocumentFragment.

I've included the source code and XML/XSL files at the end of this email

I'd be gratetful for any clues :)

Cheers,

Uwe


---- input.xml ----

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <floatlist>0 1 2 3 4 5 6 7 8</floatlist>
   <floatlist>9 10 11 12 13 14</floatlist>
</root>


---- exttest.xsl ----

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:ext="xalanexttest.Ext" version="1.0">
   <xsl:template match="/">
       <vectors>
           <xsl:for-each select="root/floatlist">
               <xsl:value-of select="ext:makevec3s(.)"/>
           </xsl:for-each>
       </vectors>
   </xsl:template>
</xsl:stylesheet>


---- Ext.java ----

package xalanexttest;

import org.w3c.dom.*;
import org.w3c.dom.bootstrap.*;

public class Ext {

   static DOMImplementation domImpl;
   static Document doc;
   static {
       try{
DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
           domImpl = reg.getDOMImplementation("XML 1.0");
doc = domImpl.createDocument("http://pachler.name/sibengine";, "foo", null);
       } catch(ClassNotFoundException ex){
       } catch(InstantiationException ex){
       } catch(IllegalAccessException ex){
       }
   }
/** Creates a new instance of Ext */
   public Ext() {
   }
/**
    * @param args the command line arguments
    */
   public static void main(String[] argv){
       org.apache.xalan.xslt.Process.main(argv);
   }
public DocumentFragment makevec3s(String in){
       DocumentFragment dfrag = doc.createDocumentFragment();
// test float array, no need to show parsing code here
       float[] floats = {0, 1, 2, 3, 4, 5, 6, 7, 8};
for(int i=2; i<floats.length; i+=3){
           Element v3Element = doc.createElement("vec3");
           String value = ""+floats[i-2]+' '+floats[i-1]+' '+floats[i];
           v3Element.setAttribute("value", value);
           dfrag.appendChild(v3Element);
       }
return dfrag;
   }
}


---- output.xml - the output that is produced by Xalan-J ----

<?xml version="1.0" encoding="UTF-8"?>
<vectors xmlns:ext="xalanexttest.Ext"/>

Reply via email to