Solved

XML & XSLT TABLE ROW STRIPING

Posted on 2010-11-30
6
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
jmatix earned 500 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

730 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