Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 17th, 2003, 08:28 AM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default complex alternate row coloring problem.

hello,
i have been trying to fix an alternate row coloring problem to no avail so i thought that it is time to ask for some help.

the problem is as follows: i want to be able to get the following result in html:
<tr bgcolor="white">
  <td>Konto</td>
  <td>#160;</td>
</tr>
<tr bgcolor="gray">
  <td>87598.46</td>
  <td>87598.80</td>
</tr>
<tr bgcolor="white">
  <td>Depot</td>
  <td>#160;</td>
</tr>
<tr bgcolor="gray">
  <td>14572.46</td>
  <td>75918.80</td>
</tr>
<tr bgcolor="white">
  <td>#160;</td>
  <td>#160</td>
</tr>
<tr bgcolor="gray">
  <td>Total:36004.7</td>
  <td>#160</td>
</tr>
<tr bgcolor="white">
  <td>#160;</td>
  <td>#160</td>
</tr>
<tr bgcolor="gray">
  <td>Festgeld</td>
</tr>
<tr bgcolor="white">
  <td>12345.46</td>
  <td>78945.80</td>
</tr>



i have been able to achieve the above layout, however due to the fact that there are three templates that
handle the rendering of each row. one to render the konto,depot and festgeld rows, one to render the
amount rows and one to render the total row, i have not been able to achieve a balanced alternate row coloring
template.

if anybody can assist that would be great. the xml and xsl snippet is below. thank you in advance

<table>
  <total_Konto_Depot>100000.00</total>
  <record>
    <asset_name>Konto</asset_name>
    <asset_children>
      <table>
        <record>
          <asset_name>87598.46</asset_name>
        </record>
        <record>
          <asset_name>87598.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
  <record>
    <asset_name>Depot</asset_name>
    <asset_children>
      <table>
        <record>
          <asset_name>14572.46</asset_name>
        </record>
        <record>
          <asset_name>75918.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
  <record>
    <asset_name>Festgeld</asset_name>
    <asset_children>
      <table>
        <record>
          <asset_name>12345.46</asset_name>
        </record>
        <record>
          <asset_name>78945.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
</table>


<xsl:variable name="parent" select="count(/table/record)"/>
<xsl:variable name="children" select="count(/table/record/asset_children/table/record)"/>
<xsl:variable name="all-records" select="/table/record"/>
<xsl:variable name="number-of-all-records" select="count($all-records)"/>

<fo:table-body table-layout="fixed">
 <xsl:apply-templates select="/table/record">
  <xsl:with-param name="productType" select="asset_name"/>
 </xsl:apply-templates>
</fo:table-body>

<xsl:template match="table/record">
 <fo:table-row space-after.optimum="10pt" font-weight="normal" font-family="Helvetica" font-size="9pt">
  <fo:table-cell number-columns-spanned="7" text-align="left">
   <fo:block font-family="Helvetica" font-weight="bold" font-size="8pt">
    <xsl:value-of select="asset_name"/>
   </fo:block>
  </fo:table-cell>
 </fo:table-row>
   <xsl:call-template name="assetOverview"/>
   <xsl:if test="position() = 2 or ($number-of-all-records = 1)">
     <xsl:call-template name="showBalanceInfo"/>
   </xsl:if>
 </xsl:template>


<xsl:template name="assetOverview" match="asset_children/table/record">
 <xsl:for-each select="asset_children/table/record">
 <fo:table-row>
   <fo:table-cell text-align="left">
    <fo:block>
      <xsl:value-of select="asset_name"/>
    </fo:block>
   </fo:table-cell>
 </fo:table-row>
 </xsl:for-each>
</xsl:template>

<xsl:template name="showBalanceInfo">
 <fo:table-row>
   <fo:table-cell text-align="left" number-columns-spanned="6" background="#e3e3e3">
     <fo:block space-after.optimum="10pt" space-before.optimum="10pt" font-family="Helvetica" font-weight="bold" font-size="8pt">
    total:
     </fo:block>
   </fo:table-cell>
    <fo:table-cell text-align="right" background="#e3e3e3">
     <fo:block space-after.optimum="10pt" space-before.optimum="10pt" font-family="Helvetica" font-weight="bold" font-size="8pt">
       <xsl:value-of select='format-number(/table/total_Konto_Depot, "#&apos;##0.00", "de-ch")'/>
     </fo:block>
    </fo:table-cell>
   </fo:table-row>
</xsl:template>

 
Old October 18th, 2003, 03:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

First of all, you forget to wrap the snippet
Code:
<fo:table-body table-layout="fixed">
 <xsl:apply-templates select="/table/record">
  <xsl:with-param name="productType" select="asset_name"/>
 </xsl:apply-templates>
</fo:table-body>
by <xsl:template match="/"> and </xsl:template>.

I think the simplest solutuion is the following:

Since you build the table rows from different templates, it's probably impossible (or is possible with complicated tricks, I don't know) to put alternating-color rows. You can gather the rows into the variable, and then put the alternating-colors trivially:

Here is the necessary code-snippet which you have to change:

original(with the corrections I've just noted above):

Code:
    <xsl:template match="/">
        <fo:table-body table-layout="fixed">
         <xsl:apply-templates select="/table/record">
          <xsl:with-param name="productType" select="asset_name"/>
         </xsl:apply-templates>
        </fo:table-body>
    </xsl:template>

change to
Code:
    <xsl:template match="/">
        <xsl:variable name="rows">
             <xsl:apply-templates select="/table/record">
              <xsl:with-param name="productType" select="asset_name"/>
             </xsl:apply-templates>
        </xsl:variable>

        <fo:table-body table-layout="fixed">
            <xsl:for-each select="exsl:node-set($rows)/*">
                <xsl:copy>
                    <xsl:for-each select="@*">
                        <xsl:copy-of select="."/>
                    </xsl:for-each>
                    <xsl:attribute name="fo:some-attribute-for-background">
                        <xsl:choose>
                            <xsl:when test="position() mod 2 = 1">
                                <xsl:text>#aaaaaa</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>#ffffff</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:copy-of select="*"/>
                </xsl:copy>
            </xsl:for-each>
        </fo:table-body>
    </xsl:template>
And also add this namespace declaration in the xsl:stylesheet element:
Code:
xmlns:exsl="http://exslt.org/common"
Note: I'm not familiar well with elements in the namespace "fo", so I've put fictive attribute "fo:some-attribute-for-background" for element "fo:table-row".

Regards,
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unsolved Complex C Structure Marshall Problem [email protected] C# 0 November 11th, 2005 06:43 AM
Complex execCommand() problem Snib Javascript 0 June 15th, 2004 06:08 PM
Complex Set problem pankaj_daga SQL Language 0 November 12th, 2003 08:47 AM
COMPLEX QUERY PROBLEM nikosdra SQL Server ASP 15 August 4th, 2003 06:17 PM
COMPLEX QUERY PROBLEM nikosdra Classic ASP Databases 2 July 28th, 2003 02:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.