This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

XSL Java Extension problem.


I am using latest version of Xalan. It appears that

<xsl:variable name="NumberOfColumns" select="java:size($ColumnList)"/>
makes NumberOfColumns of type Integer instead of int.

It is causing the exception per the following output:

___________PROGRAM OUTPUT BEGINS_____________
/xsltest/testXslTransform.xsl; Line 14; Column 17; Got here A
/xsltest/testXslTransform.xsl; Line 15; Column 17; 3
/xsltest/testXslTransform.xsl; Line 25; Column 16; Got here B
/xsltest/testXslTransform.xsl; Line 26; Column 16; 3
/xsltest/testXslTransform.xsl; Line 30;
Column 50; javax.xml.transform.TransformerException: Can not convert
#UNKNOWN (java.lang.Integer) to a number
___________PROGRAM OUTPUT ENDS_____________

I am copying the Java code, XSL code and the input file.  If I hardcode
the value of NumberOfColumns, instead of getting it fron
java.util.ArrayList.size(), the code works fine.

My questions are:

1. How to get the size of the list from within the XSL code,
without creating and calling a wrapper method for 
java.util.ArrayList.size()?

2. Reason for the follwing logic in the xsl code:
<xsl:variable name="isValidColumn" select="java:contains($ColumnList,
string(position()))"/>

I found that all my stylesheets were same but differed in the
columns (in the XML) I was processing, for example I was doing
<xsl:apply-templates select="td[4] | td[5] | td[6] | td[7]"/>
in one stylesheet vs
<xsl:apply-templates select="td[2] | td[7] | td[8]"/>
in the other.

Is there any other way to have a generic stylesheet which will
accept the column values as parameters?



Thank you for your help and suggestions.

-sm

___________JAVA CODE BEGINS_____________
package xsltest;

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import java.util.*;


public class testXslParams {

   public static
void main(String[] argv)
throws Throwable
{


   TransformerFactory factory = TransformerFactory.newInstance();
   Source xsl = new StreamSource(new File("testXslTransform.xsl"));
   Transformer x = factory.newTransformer(xsl);
   Source source = new StreamSource(new File("testXslResponse.xml"));
   Result target = new javax.xml.transform.stream.StreamResult(new
FileOutputStream("Results.xml"));

  ArrayList columnList = new java.util.ArrayList();

  columnList.add("2");
  columnList.add("7");
  columnList.add("8");

   Map map = new java.util.HashMap();
   map.put("ColumnList", columnList );
   x.setParameter( "XSL_IO_TABLE",  map );

   x.transform(source, target);

}
}

___________JAVA CODE ENDS_____________

___________XSL CODE BEGINS_____________
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0" xmlns:lxslt="http://xml.apache.org/xslt";
xmlns:java="http://xml.apache.org/xslt/java"; extension-element-prefixes="
java" exclude-result-prefixes="java">
<xsl:param name="XSL_IO_TABLE"/>
<xsl:variable name="ColumnList"
select="java:get($XSL_IO_TABLE,'ColumnList')"/>
<xsl:variable name="NumberOfColumns" select="java:size($ColumnList)"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//table/tr"/>
</xsl:template>
<xsl:template match="//table/tr[1]">
<xsl:apply-templates select="th"/>
<Point>
<PointLabel/>
<XData>0</XData>
<xsl:message>Got here A </xsl:message>
<xsl:message>
<xsl:value-of select="$NumberOfColumns"/>
</xsl:message>
<xsl:call-template name="InitYData">
<xsl:with-param name="NumberOfColumns" select="$NumberOfColumns"/>
</xsl:call-template>
</Point>
</xsl:template>
<xsl:template name="InitYData">
<xsl:param name="NumberOfColumns" select="$NumberOfColumns"/>
<xsl:message>Got here B </xsl:message>
<xsl:message>
<xsl:value-of select="$NumberOfColumns"/>
</xsl:message>
<YData>-9999</YData>
<xsl:if test="number($NumberOfColumns) &gt; 1">
<xsl:message>Got here C </xsl:message>
<xsl:message>
<xsl:value-of select="$NumberOfColumns"/>
</xsl:message>
<xsl:call-template name="InitYData">
<xsl:with-param name="NumberOfColumns" select="number($NumberOfColumns)
- 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="//table/tr[1]/th">
<xsl:variable name="isValidColumn" select="java:contains($ColumnList,
string(position()))"/>
<xsl:if test="$isValidColumn = 'true' ">
<SeriesLabel>
<xsl:value-of select="."/>
</SeriesLabel>
</xsl:if>
</xsl:template>
<xsl:template match="//table/tr[position() >1 ]">
<Point>
<XData>
<xsl:value-of select="td[1]"/>
</XData>
<xsl:apply-templates select="td"/>
</Point>
</xsl:template>
<xsl:template match="td">
<xsl:variable name="isValidColumn" select="java:contains($ColumnList,
string(position()))"/>
<xsl:if test="$isValidColumn = 'true' ">
<YData>
<xsl:choose>
<xsl:when test=". = 'NA'">-9999</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</YData>
</xsl:if>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
___________XSL CODE ENDS_____________

___________XML INPUT FILE BEGINS_____________
<?xml version="1.0" encoding="UTF-8"?>
<reply>
<reply>
<data>
<spec/>
<table>
<columns>
<column id="dateof" type="numeric"/>
<column id="a" type="numeric"/>
<column id="b" type="numeric"/>
<column id="column4" type="numeric"/>
<column id="column5" type="numeric"/>
<column id="buy" type="numeric"/>
<column id="sell" type="numeric"/>
<column id="column8" type="numeric"/>
</columns>
<tr>
<th>dateof</th>
<th>a</th>
<th>b</th>
<th>column4</th>
<th>column5</th>
<th>buy</th>
<th>sell</th>
<th>column8</th>
</tr>
<tr>
<td>20000918-00:00:00.000 CDT</td>
<td>123.250000</td>
<td>57.500000</td>
<td>2.143478</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>20000919-00:00:00.000 CDT</td>
<td>124.937500</td>
<td>57.000000</td>
<td>2.191886</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>20010910-00:00:00.000 CDT</td>
<td>96.470000</td>
<td>39.350000</td>
<td>2.451588</td>
<td>2.503074</td>
<td>2.482923</td>
<td>2.523225</td>
<td>0.020151</td>
</tr>
<tr>
<td>20010911-00:00:00.000 CDT</td>
<td>NA</td>
<td>39.350000</td>
<td>NA</td>
<td>2.500147</td>
<td>2.480830</td>
<td>2.519463</td>
<td>0.019317</td>
</tr>
<tr>
<td>20010917-00:00:00.000 CDT</td>
<td>93.340000</td>
<td>35.150000</td>
<td>2.655477</td>
<td>2.507283</td>
<td>2.480676</td>
<td>2.533889</td>
<td>0.026606</td>
</tr>
</table>
</data>
</reply>
</reply>

___________XML INPUT FILE ENDS_____________


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]