 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 30th, 2003, 04:07 PM
|
|
Registered User
|
|
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SUM operator returns a negative number
I have the following output from the XML below:
[xml]
<row>
<sum_total_octets_sec_pct>1311147.0134680134680134 6801346801346801</sum_total_octets_sec_pct>
</row>
<row>
<sum_total_octets_sec_pct>1214770.0521885521885521 8855218855218855</sum_total_octets_sec_pct>
</row>
....
[xsl applied]
<xsl:for-each select="*">
<xsl:variable name="currname" select="name()"/>
<xsl:variable name="currvalue" select="."/>
<xsl:variable name="rest" select ="$currvalue - sum(/report//row[position() <= $topNPlus]/*[name() = $currname])"/>
<xsl:call-template name="restante">
<xsl:with-param name="colname" select="$currname"/>
<!--
<xsl:with-param name="value" select="$rest"/>
-->
<xsl:with-param name="value" select="sum(/report//row[position() <= $topNPlus]/*[name() = $currname])"/>
</xsl:call-template>
</xsl:for-each>
The result of the operation select="sum(/report//row[position() <= $topNPlus]/*[name() = $currname]) is a negative number. I was waiting for a positive number!!!
Can anybody tell me what's going on?!
Thanks in advance,
Carlos Dias
|
|

November 1st, 2003, 05:51 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sounds very strange...
Post the whole stylesheet(and XML source): probably you get the negative result due to another point in your stylesheet.
Regards,
Armen
|
|

November 3rd, 2003, 10:36 AM
|
|
Registered User
|
|
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Complete XML (this is a result of a db query)
2003-11-03 14:35:34,494 [Thread-13] INFO reporterNavigator.jsp.reporttopn - XML <?xml version='1.0' encoding='iso-8859-1' ?>
<report>
<header>
<dimension name="bbras_pt">
<drillup times="0">
<fixedpoint>
<point pname="generico" value="generico" position="0"/>
</fixedpoint>
</drillup>
<drillup times="1">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3AYEAR%3D%272003%27+AND+MONTH%3D%2710%27+AND+DAY% 3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCATION%2CBBRAS_ NAME%2CBBRAS_SLOT%3Anull%7C
</assinatura>
<fixedpoint>
<point pname="generico" value="generico" position="0"/>
</fixedpoint>
</drillup>
<drillup times="2">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3AYEAR%3D%272003%27+AND+MONTH%3D%2710%27+AND+DAY% 3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCATION%2CBBRAS_ NAME%3Anull%7C
</assinatura>
<fixedpoint>
<point pname="generico" value="generico" position="0"/>
</fixedpoint>
</drillup>
<drillup times="3">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3AYEAR%3D%272003%27+AND+MONTH%3D%2710%27+AND+DAY% 3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCATION%3Anull%7 C
</assinatura>
<fixedpoint>
<point pname="generico" value="generico" position="0"/>
</fixedpoint>
</drillup></dimension>
<dimension name="time">
<drillup times="0">
<fixedpoint>
<point pname="DAY" value="1" position="3" />
</fixedpoint>
</drillup>
<drillup times="1">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3AYEAR%3D%272003%27+AND+MONTH%3D%2710%27%7CD%3Abb ras_pt%3ABBRAS_LOCATION%2CBBRAS_NAME%2CBBRAS_SLOT% 2CBBRAS_PORT%3Anull%7C
</assinatura>
<fixedpoint>
<point pname="MONTH" value="10" position="2" />
</fixedpoint>
</drillup>
<drillup times="2">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3AYEAR%3D%272003%27%7CD%3Abbras_pt%3ABBRAS_LOCATI ON%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3Anull%7 C
</assinatura>
<fixedpoint>
<point pname="YEAR" value="2003" position="1" />
</fixedpoint>
</drillup>
<drillup times="3">
<assinatura>
F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atime%3Anull %3Anull%7CD%3Abbras_pt%3ABBRAS_LOCATION%2CBBRAS_NA ME%2CBBRAS_SLOT%2CBBRAS_PORT%3Anull%7C
</assinatura>
<fixedpoint>
<point pname="generico" value="generico" position="0"/>
</fixedpoint>
</drillup> <switch>F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3Atim e%3AYEAR%2CMONTH%2CDAY%3AYEAR%3D%272003%27+AND+MON TH%3D%2710%27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3An ull%3Anull%7C</switch>
</dimension>
</header>
<payload dimension="bbras_pt" resolution="bbras_port">
<row>
<sum_total_octets_sec>18244.3038047138047138047138 047138047138</sum_total_octets_sec>
<sum_total_octets_sec_pct>1824430.3804713804713804 7138047138047138</sum_total_octets_sec_pct>
<in_octets_sec>8632.140050505050505050505050505050 505056</in_octets_sec>
<out_octets_sec>9612.16375420875420875420875420875 4208756</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(00)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM0%2F1%27+AND+BBRAS_LOCATION%3D%27Bat alha%27+AND+BBRAS_NAME%3D%27BT-BR1%27+AND+BBRAS_SLOT%3D%27NIC%2800%29%27%7C">ATM0/1</bbras_port>
</row>
<row>
<sum_total_octets_sec>18234.9161952861952861952861 952861952862</sum_total_octets_sec>
<sum_total_octets_sec_pct>1823491.6195286195286195 2861952861952862</sum_total_octets_sec_pct>
<in_octets_sec>8855.763164983164983164983164983164 983167</in_octets_sec>
<out_octets_sec>9379.15303030303030303030303030303 0303033</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(01)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM1%2F0%27%7C">ATM1/0</bbras_port>
</row>
<row>
<sum_total_octets_sec>13914.3033670033670033670033 670033670034</sum_total_octets_sec>
<sum_total_octets_sec_pct>1391430.3367003367003367 0033670033670034</sum_total_octets_sec_pct>
<in_octets_sec>4281.464006734006734006734006734006 734011</in_octets_sec>
<out_octets_sec>9632.83936026936026936026936026936 0269356</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(00)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM0%2F3%27%7C">ATM0/3</bbras_port>
</row>
<row>
<sum_total_octets_sec>13116.0469191919191919191919 191919191919</sum_total_octets_sec>
<sum_total_octets_sec_pct>1311604.6919191919191919 1919191919191919</sum_total_octets_sec_pct>
<in_octets_sec>7007.198249158249158249158249158249 158244</in_octets_sec>
<out_octets_sec>6108.84867003367003367003367003367 0033667</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(01)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM1%2F1%27%7C">ATM1/1</bbras_port>
</row>
<row>
<sum_total_octets_sec>13111.4701346801346801346801 346801346801</sum_total_octets_sec>
<sum_total_octets_sec_pct>1311147.0134680134680134 6801346801346801</sum_total_octets_sec_pct>
<in_octets_sec>6900.750505050505050505050505050505 0505</in_octets_sec>
<out_octets_sec>6210.71962962962962962962962962962 9629633</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(00)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM0%2F0%27%7C">ATM0/0</bbras_port>
</row>
<row>
<sum_total_octets_sec>12147.7005218855218855218855 218855218855</sum_total_octets_sec>
<sum_total_octets_sec_pct>1214770.0521885521885521 8855218855218855</sum_total_octets_sec_pct>
<in_octets_sec>7421.386447811447811447811447811447 811444</in_octets_sec>
<out_octets_sec>4726.31407407407407407407407407407 4074078</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(01)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM1%2F2%27%7C">ATM1/2</bbras_port>
</row>
<row>
<sum_total_octets_sec>12048.8146464646464646464646 464646464646</sum_total_octets_sec>
<sum_total_octets_sec_pct>1204881.4646464646464646 4646464646464646</sum_total_octets_sec_pct>
<in_octets_sec>7392.203030303030303030303030303030 303033</in_octets_sec>
<out_octets_sec>4656.61161616161616161616161616161 6161611</out_octets_sec>
<bbras_location>Batalha</bbras_location>
<bbras_name>BT-BR1</bbras_name>
<bbras_slot>NIC(00)</bbras_slot>
<bbras_port drill_down="F%3Afact%3ASELECTED_FACTS%3Anull%7CD%3 Atime%3Anull%3AYEAR%3D%272003%27+AND+MONTH%3D%2710 %27+AND+DAY%3D%271%27%7CD%3Abbras_pt%3ABBRAS_LOCAT ION%2CBBRAS_NAME%2CBBRAS_SLOT%2CBBRAS_PORT%3ABBRAS _PORT%3D%27ATM0%2F2%27%7C">ATM0/2</bbras_port>
</row>
<total>
<sum_total_octets_sec>100817.555589225589225589225 5892255892255</sum_total_octets_sec><sum_total_octets_sec_pct>100 81755.5589225589225589225589225589226</sum_total_octets_sec_pct><in_octets_sec>50490.9054 545454545454545454545454545454</in_octets_sec><out_octets_sec>50326.65013468013468 01346801346801346802</out_octets_sec>
</total>
</payload>
</report>
Complete xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="netstat_macros.xsl"/>
<xsl:import href="data-report-format.xsl"/>
<xsl:strip-space elements="*" />
<!--
<xsl:output encoding="ISO-8859-1" method="html" standalone="yes"/>
-->
<xsl:param name="filename">nodef.jsp</xsl:param>
<xsl:variable name="t_sum_total_octets_sec" select="/report//total/sum_total_octets_sec"/>
<xsl:template match="report">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="payload">
<xsl:variable name="actualdim"><xsl:value-of select="@dimension"/></xsl:variable>
<xsl:call-template name="printheaders">
<xsl:with-param name="addlabel" select="concat('Top ',$topNPlus)"/>
</xsl:call-template>
<tbody>
<xsl:if test="$actualdim = 'bbras_pt'">
<xsl:apply-templates select="row" mode="bbras_pt"/>
</xsl:if>
<xsl:apply-templates select="total"/>
</tbody>
</xsl:template>
<xsl:template match="row" mode="bbras_pt">
<xsl:if test="$report = 'BBRAS_TOPN_TRF' and position() <= $topNPlus">
<tr onMouseOver="this.bgColor='#BAC2CF'" onMouseOut="this.bgColor='#FFFFFF'" bgColor="#FFFFFF" align="right" id="acesso">
<xsl:if test="/report//payload/@resolution = 'bbras_port'">
<xsl:apply-templates select="bbras_port">
<xsl:with-param name="rank" select="position()"/>
</xsl:apply-templates>
</xsl:if>
<xsl:apply-templates select="bbras_vcc">
<xsl:with-param name="rank" select="position()"/>
</xsl:apply-templates>
<xsl:variable name="currow" select="."/>
<xsl:for-each select="$rcfg/report[@name = $report]//column">
<xsl:variable name="nodocurr" select="./@name"/>
<xsl:apply-templates select="$currow/child::*[name() = $nodocurr]"/>
</xsl:for-each>
</tr>
</xsl:if>
</xsl:template>
<xsl:template match="total">
<xsl:variable name="span" select="count($rcfg/report[@name = $report]//column)"/>
<xsl:variable name="span" select="$span +1"/>
<tr onMouseOver="this.bgColor='#BAC2CF'" onMouseOut="this.bgColor='#FFFFFF'" bgColor="#FFFFFF">
<td height="10" bgColor="#FFFFFF" colspan="{$span}">
<img src="../images/transp.gif" height="10" width="100%"/>
</td>
</tr>
<tr onMouseOver="this.bgColor='#BAC2CF'" onMouseOut="this.bgColor='#FFFFFF'" align="right" bgColor="#FFFFFF" id="acesso">
<td width="147" id="acesso"><b><xsl:value-of select="$config/label[@name='restante']/@description"/></b></td>
<xsl:for-each select="*">
<xsl:variable name="currname" select="name()"/>
<xsl:variable name="currvalue" select="."/>
<xsl:variable name="rest" select ="$currvalue - sum(/report//row[position() <= $topNPlus]/*[name() = $currname])"/>
<xsl:call-template name="restante">
<xsl:with-param name="colname" select="$currname"/>
<xsl:with-param name="value" select="$rest"/>
</xsl:call-template>
</xsl:for-each>
</tr>
<tr onMouseOver="this.bgColor='#BAC2CF'" onMouseOut="this.bgColor='#FFFFFF'" bgColor="#FFFFFF">
<td height="10" bgColor="#FFFFFF" colspan="{$span}">
<img src="../images/transp.gif" height="10" width="100%"/>
</td>
</tr>
<tr onMouseOver="this.bgColor='#BAC2CF'" onMouseOut="this.bgColor='#FFFFFF'" align="right" bgColor="#FFFFFF" id="acesso">
<td width="147" id="acesso"><b><xsl:value-of select="$config/label[@name='total']/@description"/></b></td>
<xsl:variable name="currow" select="."/>
<xsl:for-each select="$rcfg/report[@name = $report]//column">
<xsl:variable name="nodocurr" select="./@name"/>
<xsl:apply-templates select="$currow/child::*[name() = $nodocurr]">
<xsl:with-param name="is_total" select="'total'"/>
</xsl:apply-templates>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match="bbras_port|bbras_vcc">
<xsl:param name="rank"/>
<td id="acessotopn">
<xsl:if test="$rank">
<xsl:value-of select="$rank"/> -
</xsl:if>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template name="restante">
<xsl:param name="colname"/>
<xsl:param name="value"/>
<td align="right" id="acesso">
<xsl:choose>
<!--
<xsl:when test="$colname = 'sum_total_octets_sec' or
$colname = 'in_octets_sec' or
$colname = 'out_octets_sec'">
-->
<!--
<xsl:when test="$colname = 'sum_total_octets_sec'">
<xsl:value-of select="format-number(number($value) div 1000,'##.##0,00','formato-europeu-decimal')"/> -
<xsl:value-of select="$value"/>
</xsl:when>
-->
<xsl:when test="$colname = 'sum_total_octets_sec_pct'">
<!--
<xsl:value-of select="format-number(number(substring-before($value,'.')) div number(substring-before($t_sum_total_octets_sec,'.')),'##0,00','for mato-europeu-decimal')"/> %
-->
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($value,'.')"/>
</xsl:otherwise>
</xsl:choose>
</td>
</xsl:template>
<xsl:template match="sum_total_octets_sec_pct">
<td align="right" id="acesso">
<xsl:if test="boolean(text())">
<xsl:value-of select="format-number(number(substring-before(text(),'.')) div number(substring-before($t_sum_total_octets_sec,'.')),'##0,00','for mato-europeu-decimal')"/> %
</xsl:if>
</td>
</xsl:template>
</xsl:stylesheet>
Thanks in advance,
Carlos Dias
|
|

November 5th, 2003, 04:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh sorry... I forgot about this thread... I've just seen that you posted the stuff.
The source is not small of course :), but I'll try to analyze it.
Regards,
Armen
|
|

November 5th, 2003, 04:57 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OK, I've written a small stylesheet to see the output. I've dropped all templates but
for "total". I get expected output with Saxon 6.5.2. The stylesheet below is applied to the
full XML source you posted recently.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="topNPlus" select="count(/report/payload/row)"/>
<xsl:template match="/">
<xsl:apply-templates select="/report/payload/total"/>
</xsl:template>
<xsl:template match="total">
<xsl:for-each select="*">
<xsl:variable name="currname" select="name()"/>
<xsl:variable name="currvalue" select="."/>
<xsl:variable name="s" select="sum(/report/payload/row[position() <= $topNPlus]/*[name() = $currname])"/>
<xsl:variable name="rest" select="$currvalue - $s"/>
<p>
'rest' for "<xsl:value-of select=" $currname"/>" = <xsl:value-of select="$currvalue"/> - <xsl:value-of select="$s"/> = <xsl:value-of select="$rest"/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And I get
Code:
<p>'rest' for "sum_total_octets_sec" = 100817.5555892255892255892255892255892255 - 100817.55558922558 = 0.000000000014551915228366852</p>
<p>'rest' for "sum_total_octets_sec_pct" = 10081755.5589225589225589225589225589226 - 10081755.558922559 = 0</p>
<p>'rest' for "in_octets_sec" = 50490.9054545454545454545454545454545454 - 50490.90545454546 = 0</p>
<p>'rest' for "out_octets_sec" = 50326.6501346801346801346801346801346802 - 50326.65013468014 = 0</p>
I guess the output is OK...
What XSLT processor you are using?
Regards,
Armen
|
|

November 5th, 2003, 07:58 AM
|
|
Registered User
|
|
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Armen for your replay!
I test the stylesheet form your post using the xalan parser directaly to the xml and works fine.
But using the stylesheet with JAXP I have the same problem!!!
Perhaps the problem as to with the class that I use to transform
the XML.
Here is the complete class:
package report.beans;
// Imported TraX classes
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationExcept ion;
// Imported java.io classes
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;
import java.beans.*;
/**
* Description of the Class
*
*@author CDias
*@created 3 de Julho de 2003
*/
public class DataTransformer {
//Properties
private String workdir = "";
private String basePath = "";
private String htmlresult = "";
private String xmlinput = "";
private String stylesheet = "";
private Hashtable parameter = new Hashtable();
protected PropertyChangeSupport plisteners = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener l) {
plisteners.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListene r l) {
plisteners.removePropertyChangeListener(l);
}
public void firePropertyChangeEvt(String p_name, String oldState, String newState) {
plisteners.firePropertyChange(p_name, oldState, newState);
}
public void setParameter(String param) {
StringTokenizer tk = new StringTokenizer(param, "&");
while (tk.hasMoreTokens()) {
StringTokenizer tk1 = new StringTokenizer((tk.nextToken()).toString(), "=");
String name = (tk1.nextToken()).toString();
String value = (tk1.nextToken()).toString();
parameter.put(name, value);
}
}
//String xml a transformar
public void setWorkdir(String dir) {
System.setProperty("user.dir", dir);
}
public void setXmlinput(String xml) {
this.xmlinput = new String(xml);
}
//Desvolve a string currende de xml que se pretende transformar
public String getXmlinput() {
return new String(xmlinput);
}
//Set do nome do ficheiro que processa a stylesheet
public void setStylesheet(String ssh) {
this.stylesheet = ssh;
}
public void setBasePath(String bp) {
basePath = bp;
}
//Devolve o nome da stylesheet que esta a ser usada
public String getStylesheet() {
return new String(stylesheet);
}
//Devolve o html corresponde ao xml de acordo com a stylesheet que esta a ser usada no momento
public String getHtmlresult() throws TransformerException, TransformerConfigurationException {
String media = null;
String title = null;
String charset = null;
try {
//firePropertyChangeEvt("XMLSTART",new java.util.Date().toString(),"");
byte b[] = xmlinput.getBytes();
InputStream ba = new ByteArrayInputStream(b);
StreamSource sc = new StreamSource(ba);
TransformerFactory tFactory = TransformerFactory.newInstance();
Source xslSource = new StreamSource(new URL(stylesheet).openStream());
xslSource.setSystemId(basePath);
//DEBUG
Transformer transformer = tFactory.newTransformer(xslSource);//
//Inicializar parametros para a Stylesheet
Enumeration chaves = parameter.keys();
while (chaves.hasMoreElements()) {
String c = (String) chaves.nextElement();
String v = (String) parameter.get(c);
transformer.setParameter(c.trim(), v.trim());
}
parameter.clear();//Clean up Hashtable
ByteArrayOutputStream os = new ByteArrayOutputStream();
transformer.transform(sc, new StreamResult(os));
//DEBUG
firePropertyChangeEvt("XMLEND", new java.util.Date().toString(), "");
return (os.toString()).trim();
} catch (Exception e) {
e.printStackTrace();
throw new TransformerException(e.getMessage());
}
}
}
Thanks in advance,
Carlos Dias
|
|

November 5th, 2003, 08:57 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Probably there is a bug in Xalan implementation of JAXP, i don't know for sure although... try to look at their bug-lists.
You can use Xalan directly. The main drawback is that in this case the code becomes bound to
specific XSLT processor. Or you can jump to another JAXP implementation without changing your code. Try both if you have time.
In any case, here is a method which performs a transformation(defined in some class called Utilities):
Code:
public static StringWriter transform(XSLTInputSource xmlSource, XSLTInputSource xslSheet, Vector stylesheetParameters) throws org.xml.sax.SAXException {
StringWriter sw = new StringWriter();
// use XSLTProcessorFactory to instantiate an XSLTProcessor
XSLTProcessor processor = org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor();
XSLTResultTarget xmlResult = new XSLTResultTarget(sw);
// set the stylesheet parameters
if(stylesheetParameters != null) {
StylesheetParameter currParam;
for(Iterator it = stylesheetParameters.iterator(); it.hasNext(); ) {
currParam = (StylesheetParameter)it.next();
processor.setStylesheetParam(currParam.getName(), currParam.getValue());
}
}
// perform the transformation
processor.process(xmlSource, xslSheet, xmlResult);
return sw;
}
here the StylesheetParameter is a class with simple functionality, you can write it very quickly.
Then you can call the method this way:
Code:
try {
transformationOutput = Utilities.transform(new XSLTInputSource(xmlInput), new XSLTInputSource(xslInput), null);
} catch(org.xml.sax.SAXException se) {
se.printStackTrace();
}
Regards,
Armen
|
|

December 31st, 2004, 05:08 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Did you get that to work? I have some version issues here. Xalan 2.x does not have those XSLT classes. Xalan 1.x has them.
When I tried to use Xalan 1.x, Xerces classes are throwing errors ...
Thanks for any help.
|
|
 |