RE: [xsl] Different Colors for Alternating Rows

Subject: RE: [xsl] Different Colors for Alternating Rows
From: "Schwartz, Rechell R, ALABS" <rrschwartz@xxxxxxx>
Date: Wed, 25 Jun 2003 21:50:17 -0500
Steven,

Thank you for your solution. I fixed the stack problem, but still, I got
back only partial results (although fairly quickl), and then the program
aborted with the following message:

Rechell Schwartz

<Jun 25, 2003 10:30:57 PM EDT> <Info> <GUI> <maeder> <FM>
<ExecuteThread: '9' for queue: 'default'> <> <> <000000> <data.jsp::XS
L Transformation Exception:java.lang.ArrayIndexOutOfBoundsException>
####<Jun 25, 2003 10:30:57 PM EDT> <Info> <GUI> <maeder> <FM>
<ExecuteThread: '9' for queue: 'default'> <> <> <000000> <javax.xml.tr
ansform.TransformerException: java.lang.ArrayIndexOutOfBoundsException
        at
weblogic.apache.xalan.transformer.TransformerImpl.transformNode(Transfor
merImpl.java:1248)
        at
weblogic.apache.xalan.transformer.TransformerImpl.transform(TransformerI
mpl.java:483)
        at
weblogic.apache.xalan.transformer.TransformerImpl.transform(TransformerI
mpl.java:1153)
        at jsp_servlet._maint.__data._jspService(__data.java:251)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:265)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:304)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:200)
        at
weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServl
etContext.java:2495)
        at
weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.
java:2204)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
---------
java.lang.ArrayIndexOutOfBoundsException 
        at
weblogic.apache.xpath.XPathContext.popCurrentNode(XPathContext.java:552)
        at
weblogic.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Tr
ansformerImpl.java:2100)
        at
weblogic.apache.xalan.transformer.TransformerImpl.transformNode(Transfor
merImpl.java:1225)
        at
weblogic.apache.xalan.transformer.TransformerImpl.transform(TransformerI
mpl.java:483)
        at
weblogic.apache.xalan.transformer.TransformerImpl.transform(TransformerI
mpl.java:1153)
        at jsp_servlet._maint.__data._jspService(__data.java:251)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:27) 
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:265)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:304)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
java:200)
        at
weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServl
etContext.java:2495)
        at
weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.
java:2204)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
>
####<Jun 25, 2003 10:30:57 PM EDT> <Error> <HTTP> <maeder> <FM>
<ExecuteThread: '9' for queue: 'default'> <> <> <101020> <[WebAppSer
vletContext(3320752,DefaultWebApp_FM,/DefaultWebApp_FM)] Servlet failed
with Exception>
java.lang.IllegalStateException: response already committed
        at
weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletRespon
seImpl.java:495)
        at jsp_servlet._maint.__data._jspService(__data.java:258)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)

-----Original Message-----
From: Kienle, Steven C [IT/0200] [mailto:steven.c.kienle@xxxxxxxxxxxxx] 
Sent: Tuesday, June 24, 2003 2:03 PM
To: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: RE: [xsl] Different Colors for Alternating Rows



The following template does what you want it to do, I think.  It isn't
pretty and basically takes control of the recursion of the sibling tr
nodes.
That is, it using the apply-templates but only one node at a time.
Because
of that, I have no idea what the actual performance would be.  I also
removed the <xsl:for-each select="td[1]"> because those really only
iterate
once and therefore the td[1] can be used in the select XPath for the
apply-templates element.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="node()|@*">
        <!--identity transform-->
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="table">
        <xsl:copy>
            <xsl:choose>
                <xsl:when test="tr[1][td[not(a) and not(@class)]]">
                    <xsl:apply-templates 
                     select="tr[1]" 
                     mode="color-change">
                        <xsl:with-param 
                         name="odd-row" 
                         select="true()"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates 
                     select="tr[1]" 
                     mode="copy-only">
                        <xsl:with-param 
                         name="odd-row" 
                         select="true()"/>
                    </xsl:apply-templates>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="tr" mode="copy-only">
        <xsl:param name="odd-row"/>

        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>

        <xsl:choose>
            <xsl:when test="following-sibling::tr[1][td[not(a) and
not(@class)]]">
                <xsl:apply-templates 
                  select="following-sibling::tr[1]" 
                  mode="color-change">
                    <xsl:with-param 
                     name="odd-row" 
                     select="$odd-row"/>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates 
                 select="following-sibling::tr[1]" 
                 mode="copy-only">
                    <xsl:with-param 
                     name="odd-row" 
                     select="$odd-row"/>
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="tr" mode="color-change">
        <xsl:param name="odd-row"/>

        <xsl:copy>
            <xsl:choose>
                <xsl:when test="$odd-row">
                    <td class="oddMedium" width="35%">
                        <xsl:apply-templates 
                         select="td[1]/node()|@*"/>
                    </td>
                    <td class="oddMedium" width="65%">
                        <xsl:apply-templates 
                         select="td[2]/node()|@*"/>
                    </td>
                </xsl:when>
                <xsl:otherwise>
                    <td class="evenMedium" width="35%">
                        <xsl:apply-templates 
                         select="td[1]/node()|@*"/>
                    </td>
                    <td class="evenMedium" width="65%">
                        <xsl:apply-templates 
                         select="td[2]/node()|@*"/>
                    </td>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>

        <xsl:choose>
            <xsl:when test="following-sibling::tr[1][td[not(a) and
not(@class)]]">
                <xsl:apply-templates 
                 select="following-sibling::tr[1]" 
                 mode="color-change">
                    <xsl:with-param 
                     name="odd-row" 
                     select="not($odd-row)"/>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates 
                 select="following-sibling::tr[1]" 
                 mode="copy-only">
                    <xsl:with-param 
                     name="odd-row" 
                     select="not($odd-row)"/>
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

-----Original Message-----
From: Schwartz, Rechell R, ALABS [mailto:rrschwartz@xxxxxxx]
Sent: Tuesday, June 24, 2003 8:46 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Different Colors for Alternating Rows


All,

I have bee using the following stylesheet to produce different colors
for
alternating rows whenever a row that has <td> tags without attributes or
hyperlinks are encountered. The problem is for large documents the
performance degrades significantly. This seems to be because in the
algorithm used to calculate which color to use, a check is made to
determine
how many previous siblings have already been processed, so the number of
checks grows exponentially as the number of rows grows.

It seems that that the same effect should be possible with greatly
improved
performance by recursively incrementing a variable whenever a row that
is a
candidate for a color change has been processed. I was unsure of how to
go
about doing this. Any code snippets would be greatly appreciated.
Following
is my stylesheets, sample HTML, and desire output:

Thanks,
Rechell Schwartz

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                   version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template match="node()|@*"> <!--identity transform-->
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
</xsl:template>

<xsl:template match="table/tr[td[not(a) and not(@class)]]">
        <xsl:copy>
     <xsl:choose>
     <xsl:when test="count( preceding-sibling::tr[td[not(a)  and 
not(@class)]] ) mod 2 = 1">
     <xsl:for-each select="td[1]">
      <td class="evenMedium" width="35%">
       <xsl:apply-templates select="node()|@*"/>
       </td>
      </xsl:for-each>
      <xsl:for-each select="td[2]">
      <td class="evenMedium" width="65%">
       <xsl:apply-templates select="node()|@*"/>
       </td>
      </xsl:for-each>
     </xsl:when>
     <xsl:otherwise>
     <xsl:for-each select="td[1]">
       <td class="oddMedium" width="35%">
       <xsl:apply-templates select="node()|@*"/>
       </td>
      </xsl:for-each>
       <xsl:for-each select="td[2]">
       <td class="oddMedium" width="65%">
       <xsl:apply-templates select="node()|@*"/>
       </td>
      </xsl:for-each>
     </xsl:otherwise>
     </xsl:choose>
       </xsl:copy>
</xsl:template>
</xsl:stylesheet>

The input file is:

<table>
<tr>
<td class="headerStyle" colspan="2">
Title
</td>
</tr>
<tr>
<td>Label1</td>
<td>Value1</td>
</tr>
<tr>
<td class="separatorStyle colspan="2">
---------------
</td>
</tr>
<tr>
<td>Label2</td>
<td>Value2</td>
</tr>
</table>

The desired output:

<table>
<tr>
<td class="headerStyle" colspan="2">
Title
</td>
</tr>
<tr>
<td class="oddRowStyle" width="35%">Label1</td>
<td class="oddRowStyle" width="65%">Value1</td>
</tr>
<tr>
<td class="separatorStyle colspan="2">
---------------
</td>
</tr>
<tr>
<td class="evenRowStyle" width="35%">Label2</td>
<td class="evenRowStyle" width="65%">Value2</td>
</tr>
</table>


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

This communication is intended solely for the use of the addressee and
may
contain information that is legally privileged, confidential or exempt
from
disclosure.  If you are not the intended recipient, please note that any

dissemination, distribution, or copying of this communication is
strictly 
prohibited.  Anyone who receives this message in error should notify the

sender immediately and delete it from his or her computer.


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


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


Current Thread