?
Solved

XML & XSLT TABLE ROW STRIPING

Posted on 2010-11-30
6
Medium Priority
?
571 Views
Last Modified: 2012-05-10
I have a HTML table produced through xslt, to display xml results. i am trying to have alternating background colours on each row of the table. I cant get it to work.

Code provided in beloew section. Just wondered if anyone can shed light on the matter. As I really don't get whats going wrong.

Here is where I call the xsl variable, which I believe should then allocate its content to the classname.

<xsl:template match="cdrRecord">
        <tr class="{$trTester}">


Thank you.
Below is xm l snippet:

<topCallsData>
  <xTop25Calls>
    <cdrRecord startDate="11/02/2008" startTime="06:18:00" originator="0008" destination="O2" dialledNumber="07708572886" duration="00:02:00" cost="0.47" />
    <cdrRecord startDate="10/02/2008" startTime="17:33:44" originator="0003" destination="Edinburgh" dialledNumber="01314572884" duration="00:25:36" cost="0.39" />
    <cdrRecord startDate="10/02/2008" startTime="22:16:44" originator="0007" destination="Edinburgh" dialledNumber="01314572888" duration="00:20:36" cost="0.32" />
    <cdrRecord startDate="12/02/2008" startTime="17:01:00" originator="0003" destination="Redditch" dialledNumber="01527572886" duration="00:02:30" cost="0.24" />
    <cdrRecord startDate="12/02/2008" startTime="09:04:00" originator="0007" destination="Maidstone" dialledNumber="01622374859" duration="00:02:04" cost="0.12" />
    <cdrRecord startDate="10/02/2008" startTime="22:26:44" originator="0002" destination="O2" dialledNumber="07842536748" duration="00:00:36" cost="0.12" />
    <cdrRecord startDate="10/02/2008" startTime="11:33:44" originator="0004" destination="Edinburgh" dialledNumber="01314572885" duration="00:05:36" cost="0.09" />
    <cdrRecord startDate="12/02/2008" startTime="07:19:00" originator="0008" destination="Bolton" dialledNumber="01204572886" duration="00:00:23" cost="0.08" />
    <cdrRecord startDate="10/02/2008" startTime="22:33:44" originator="0006" destination="Edinburgh" dialledNumber="01314572887" duration="00:03:36" cost="0.06" />
    <cdrRecord startDate="13/02/2008" startTime="04:17:00" originator="2222" destination="Medway" dialledNumber="01634572886" duration="00:02:13" cost="0.03" />
  </xTop25Calls>
</topCallsData>

Here is xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:variable name="trTester">
    <xsl:choose>
      <xsl:when test="position() mod 2 = 0">even</xsl:when>
      <xsl:otherwise>odd</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:template match="topCallsData">
    <div id="topCallsDiv">
      <div class="topCallsHeaderSection">
        <div class="reportHeader">TopCalls</div>
        <br />
        <table class="tblTopCallsCriteria" >
          <tr>
            <td class="tdCellHeader">Top Calls Period:</td>
            <td class="tdCellContent" id="tdPeriod" ></td>
          </tr>
          <tr>
            <td class="tdCellHeader">Top Calls Date:</td>   
            <td class="tdCellContent" id="tdDate" ></td>
          </tr>
        </table>      
      </div>
            <xsl:apply-templates />
    </div>
  </xsl:template>

  <xsl:template match="xTop25Calls">
    <table  class="tblBilling">
      <tr class="tblHeader">
        <td class="tdDate">Start Date</td>
        <td class="tdTime">Start Time</td>
        <td class="tdOriginator">Originator</td>
        <td class="tdDestination">Destination</td>
        <td class="tdDialled">Dialled Number</td>
        <td class="tdDuration">Duration</td>
        <td class="tdCost">Cost</td>
      </tr>
      <xsl:apply-templates />
    </table>
  </xsl:template>
  
  <xsl:template match="cdrRecord"> 
        <tr class="{$trTester}">
          <td>
            <xsl:value-of select="@startDate"/>
          </td>
          <td>
            <xsl:value-of select="@startTime"/>
          </td>
          <td>
            <xsl:value-of select="@originator"/>
          </td>
          <td>
            <xsl:value-of select="@destination"/>
          </td>
          <td>
            <xsl:value-of select="@dialledNumber"/>
          </td>
          <td>
            <xsl:value-of select="@duration"/>
          </td>
          <td>
            <xsl:value-of select="@cost"/>
          </td>
        </tr>
  </xsl:template>
  </xsl:stylesheet>


Here is corresponding CSS:

.odd 
{
    background-color:Blue;
}

.even
{
    background-color:Red;
}

Open in new window

0
Comment
Question by:gillsboy123
  • 3
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
Justin Mathews earned 2000 total points
ID: 34239592
position() function in variable declaration might be causing problem. Try this xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="topCallsData">
    <div id="topCallsDiv">
      <div class="topCallsHeaderSection">
        <div class="reportHeader">TopCalls</div>
        <br />
        <table class="tblTopCallsCriteria" >
          <tr>
            <td class="tdCellHeader">Top Calls Period:</td>
            <td class="tdCellContent" id="tdPeriod" ></td>
          </tr>
          <tr>
            <td class="tdCellHeader">Top Calls Date:</td>   
            <td class="tdCellContent" id="tdDate" ></td>
          </tr>
        </table>      
      </div>
            <xsl:apply-templates />
    </div>
  </xsl:template>

  <xsl:template match="xTop25Calls">
    <table  class="tblBilling">
      <tr class="tblHeader">
        <td class="tdDate">Start Date</td>
        <td class="tdTime">Start Time</td>
        <td class="tdOriginator">Originator</td>
        <td class="tdDestination">Destination</td>
        <td class="tdDialled">Dialled Number</td>
        <td class="tdDuration">Duration</td>
        <td class="tdCost">Cost</td>
      </tr>
      <xsl:apply-templates />
    </table>
  </xsl:template>
  
  <xsl:template match="cdrRecord"> 
        <tr>
		 <xsl:attribute name="class">
			 <xsl:choose>
			  <xsl:when test="position() div 2 mod 2 = 0">even</xsl:when>
			  <xsl:otherwise>odd</xsl:otherwise>
			 </xsl:choose>
		 </xsl:attribute>
          <td>
            <xsl:value-of select="@startDate"/>
          </td>
          <td>
            <xsl:value-of select="@startTime"/>
          </td>
          <td>
            <xsl:value-of select="@originator"/>
          </td>
          <td>
            <xsl:value-of select="@destination"/>
          </td>
          <td>
            <xsl:value-of select="@dialledNumber"/>
          </td>
          <td>
            <xsl:value-of select="@duration"/>
          </td>
          <td>
            <xsl:value-of select="@cost"/>
          </td>
        </tr>
  </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:gillsboy123
ID: 34240158
Worked a treat. just had to change the when test too:

<xsl:when test="position() mod 2 = 0">even</xsl:when>

You are a Star!!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34240169
two problems, jmatix only fixed one and then did something very ugly to make the stylesheet work
May I recommend that you never ever do this?
                    <xsl:when test="position() div 2 mod 2 = 0">even</xsl:when>
you simply can't rely on the whitespace only nodes to be only counted once (break and spaces could be multiple text nodes and then you are doomed)

problem one.
There is no reason why position() in a variable should not work. (why would it jmatix?)
but you need to put the variable in context of the nodeset being counted

problem 2
if you use position make sure that the nodeset you are working on only has the nodes you want to count
don't forget that white-space nodes make it to the nodeset counted too,
so be specific in the apply-templates higher up the tree

Below stylesheet really fixes both problems, changes are commented

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:template match="topCallsData">
        <div id="topCallsDiv">
            <div class="topCallsHeaderSection">
                <div class="reportHeader">TopCalls</div>
                <br />
                <table class="tblTopCallsCriteria" >
                    <tr>
                        <td class="tdCellHeader">Top Calls Period:</td>
                        <td class="tdCellContent" id="tdPeriod" ></td>
                    </tr>
                    <tr>
                        <td class="tdCellHeader">Top Calls Date:</td>   
                        <td class="tdCellContent" id="tdDate" ></td>
                    </tr>
                </table>      
            </div>
            <xsl:apply-templates />
        </div>
    </xsl:template>
    
    <xsl:template match="xTop25Calls">
        <table  class="tblBilling">
            <tr class="tblHeader">
                <td class="tdDate">Start Date</td>
                <td class="tdTime">Start Time</td>
                <td class="tdOriginator">Originator</td>
                <td class="tdDestination">Destination</td>
                <td class="tdDialled">Dialled Number</td>
                <td class="tdDuration">Duration</td>
                <td class="tdCost">Cost</td>
            </tr>
            <xsl:apply-templates select="cdrRecord"/><!-- problem two fixed by being specific -->
        </table>
    </xsl:template>
    
    <xsl:template match="cdrRecord"> 
        <!-- problem one solved by putting  the variable in scope-->
        <xsl:variable name="trTester">
            <xsl:choose>
                <xsl:when test="position() mod 2 = 0">even</xsl:when>
                <xsl:otherwise>odd</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <tr class="{$trTester}">
            <td>
                <xsl:value-of select="@startDate"/>
            </td>
            <td>
                <xsl:value-of select="@startTime"/>
            </td>
            <td>
                <xsl:value-of select="@originator"/>
            </td>
            <td>
                <xsl:value-of select="@destination"/>
            </td>
            <td>
                <xsl:value-of select="@dialledNumber"/>
            </td>
            <td>
                <xsl:value-of select="@duration"/>
            </td>
            <td>
                <xsl:value-of select="@cost"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34240222
You should not say he is a star based on that particular solution :-)

The reason why I said that jmatix's solution was not good is that you needed to change that particular line
you are likely using a different processor than jmatix was. I bet you are using msxml
msxml throws away nodes that are kept in all other processors.

You have just proven the point why I said it was a bad solution :-)

So while jmatix had to do a lausy trick to not count these whit-space only text nodes, you could throw out that hack since
- you are either using msxml (msxml makes the error of throwing them out without thinking)
- or you were doing it the right way, being selective in the apply-templates in your final template

please be selective, your stylesheet will stop working if you change processor or if microsoft starts doing it the right way finally
0
 

Author Comment

by:gillsboy123
ID: 34240319
Thanks for your advise Gertone. I hastily gave the points away. In the end i used your approach, and thanks for the advise about using the variable in scope. Thats where I was coming up short. Will have to bear that in mind. I'm slowly catching on the xslt.

Also have been more specific with <xsl:apply-template select="cdrRecord"/>, in any case is the latter just the same as <xsl:call-template name="cdrRecord />

Is there any way to re-allocate the points??
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34240895
well, I don't care about the points, but since the database is used for a lot of people to learn, there is a good reason for reallocating and at least accept the proper answer.
You can always ask assistance, ask for breaking open the question and accept the right answer.
Don't do it for me, since I really don't worry about points, but do for those reading the question later.
If you decide to do so, please have jmatix's answer as an assisting answer and give him the bulk of the points. After all there was good stuff in there too

Your follow up question is a bit tricky
in your current stylesheet you should use apply-templates
using apply-templates you pass nodes to the templates
with call-template you make a call to a named template (that is similar to a function)
you could imagine that you give all named templates the name of an element and since the current context is passed as well...
all too tricky.
Just understand that the call-template calls a method/function/procedure and apply-templates passes nodes for evaluation by matching templates,
and don't try to use it in a much different way
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question