Before I throw in the towel on this one, I tried my own approach, which I
thought should work, but didn't. Any insights would be appreciated. Basically,
I was trying to select all rows. If they qualified for color, then I
incremented a parameteter. If they didn't, I just made a copy of them. Here is
the code I used:
<?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="tr">
<xsl:param name="rows" select="0"/>
<xsl:copy>
<xsl:choose>
<xsl:when test="td[not(a) and not(@class)]">
<xsl:choose>
<xsl:when test="$rows 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:apply-templates select="node()|@*">
<xsl:with-param name="rows" select="$rows + 1" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Here is the undesired result I received.
<tr>
<td width="35%" class="oddMedium">Label 1:</td><td width="65%"
class="oddMedium">Value 1</td><td>Label 1:</td><td>Value 1</td>
</tr>
<tr>
<td width="35%" class="oddMedium">Label 2:</td><td width="65%"
class="oddMedium">Value 2</td><td>Label 2:</td><td>Value 2</td>
</tr>
<tr>
-----Original Message-----
From: Schwartz, Rechell R, ALABS
Sent: Thursday, June 26, 2003 9:36 AM
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: RE: [xsl] Different Colors for Alternating Rows
This won't work because I don't know in advance where the separtors will be
because each section can be dynamic. Also, there could be up to tow successive
rows that don't qualify for alternating colors. I don't know why this line is
so bad. Aren't you only looking back at one previous sibling for each element?
That should only be 0(2n).
xsl:copy-of select="preceding-sibling::tr[1][td[a or @class]]" />
Rechell Schwartz
-----Original Message-----
From: Zink, Juergen [mailto:Juergen(_dot_)Zink(_at_)wuerttembergische(_dot_)de]
Sent: Thursday, June 26, 2003 4:45 AM
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: RE: [xsl] Different Colors for Alternating Rows
Importance: Low
Rechell,
This looked like a nice approach, but it didn't offer any performance
improvement over what I had before, to my surprise. This is the line that
seems to be the killer:
<xsl:copy-of select="preceding-sibling::tr[1][td[a or @class]]" />
To avoid the statement above, you can try to insert the separator with
modulo calculation
and the table header with the beginning. Meaning you ignore the separator
from the xml-file
and generate your own separator. Is this Ok?
Something like
<xsl:if test="position() mod X>
<td class="separatorStyle colspan="2">---------------</td>
</xsl:if>
Then it should be o(n).
Juergen
-----Original Message-----
From: Zink, Juergen [mailto:Juergen(_dot_)Zink(_at_)wuerttembergische(_dot_)de]
Sent: Wednesday, June 25, 2003 2:27 AM
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: RE: [xsl] Different Colors for Alternating Rows
Importance: Low
Hi Rechell,
can you use the following simple iterative approach?
<xsl:template match="/">
<table>
<xsl:for-each select="table/tr[td[not(a or @class)]]">
<!-- keep separator and header ... -->
<xsl:copy-of select="preceding-sibling::tr[1][td[a or @class]]" />
<xsl:variable name="attrClass">
<xsl:choose>
<xsl:when test="position() mod 2">evenMedium</xsl:when>
<xsl:otherwise>oddMedium</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr>
<td class="{$attrClass}" width="35%">
<xsl:copy-of select="td[1]/node()|td[1]/@*"/>
</td>
<td class="{$attrClass}" width="65%">
<xsl:copy-of select="td[1]/node()|td[1]/@*"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
Juergen
-----Ursprüngliche Nachricht-----
Von: Schwartz, Rechell R, ALABS [mailto:rrschwartz(_at_)att(_dot_)com]
Gesendet am: Mittwoch, 25. Juni 2003 04:59
An: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Betreff: RE: [xsl] Different Colors for Alternating Rows
Steven,
I tried the solution and am getting the same crashing problem as I had
with the other solution that was posted. Any idea what is wrong? One
time I ran it, and I got some output to the screen, but then I keep
getting the crashing.
Thanks,
Rechell Schwartz
t
weblogic.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemp
lates.java:193)
at
weblogic.apache.xalan.transformer.TransformerImpl.executeChildTemplates(
TransformerImpl.java:2208)
at
weblogic.apache.xalan.templates.ElemChoose.execute(ElemChoose.java:152)
at
weblogic.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemF
orEach.java:498)
at
weblogic.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemp
lates.java:193)
at
weblogic.apache.xalan.transformer.TransformerImpl.executeChildTemplates(
TransformerImpl.java:2208)
-----Original Message-----
From: Kienle, Steven C [IT/0200]
[mailto:steven(_dot_)c(_dot_)kienle(_at_)pharmacia(_dot_)com]
Sent: Tuesday, June 24, 2003 2:03 PM
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
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(_at_)att(_dot_)com]
Sent: Tuesday, June 24, 2003 8:46 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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
****************************************************************************
*
Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese
eMail irrtuemlich erhalten haben, informieren Sie bitte sofort den
Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren
sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.
This e-mail may contain confidential and/or privileged information.
If you are not the intended recipient (or have received this e-mail
in error) please notify the sender immediately and destroy this e-mail.
Any unauthorized copying, disclosure or distribution of the material
in this e-mail is strictly forbidden.
****************************************************************************
*
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list