• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

XSL TO ALTERNATE TABLE ROW COLORS

THIS IS THE XSL I USE TO DISPLAY THE RESULTS. BUT I WANT TO ALTERNATE THE TABLE RECORDS
<?xml version='1.0' encoding='iso-8859-1'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>



<!-- RIQ_report.xsl: a stylesheet for all organization, security, canned reports of RIQ project -->

<!-- Import Global Template Here -->
<xsl:import href="../GlobalTemplate.xsl"/>

<xsl:output method='html'  encoding='iso-8859-1' indent='no'/>

<!-- Implement Page-Specific Templates -->
<!-- Title -->
<xsl:template name="PageTitle">
      Standard &amp; Poor's: Ratings iQuery Search Results
</xsl:template>

<!-- Page Content -->

<!-- Begin Main Template -->
<xsl:template name="PageContent">
<link rel="stylesheet" type="text/css" href="/include/rrm_export.css"/>

<script language="JavaScript" type="text/javascript">
function exportReport(xlsRepURL, ascRepURL)
{
  //alert("xlsRepURL=" + xlsRepURL);
  //alert("ascRepURL=" + ascRepURL);
  var len = 0;
  len = document.all.expFormat.length;
  if (len > 1) {
    var rptFmtOpt = document.all.expFormat.options;
    var selIndex = 0;
    // selectedIndex = 0 is select export format; 1 is Excel; 2 is XML report
    if (rptFmtOpt != undefined) {
      selIndex = rptFmtOpt.selectedIndex;
    } else {
      selIndex = 0;
    }

    if (selIndex == 0) {
      alert("Please Select Export Format from the dropdown list");
      return;
    }

    if (selIndex == 1) {
      window.open(xlsRepURL,'ExcelReport','toolbar=no,width=700,height=400,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=yes');
    }

    if (selIndex == 2) {
      window.open(ascRepURL,'ASCIIReport','toolbar=no,width=700,height=400,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=yes');
    }
  }
}

<xsl:comment><![CDATA[
function myFunction()
{
 if(document.report_results.chkbox.checked)
 {
  var doc = document.report_results;
  var a = doc.elements.length;

  for (i = 0; i < a; i++)
  {
    if(doc[i].type == "checkbox");
    doc[i].checked = true;
   }
 }
 else
 {
   var doc = document.report_results;
   var a = doc.elements.length;
   for (i = 0; i < a; i++)
   {
     if(doc[i].type == "checkbox")
     doc[i].checked = false;
   }
 }
}
]]>
</xsl:comment>
</script>
<P></P>
<table width="900" cellpadding="1" cellspacing="1" border="0">
<tr>
<td width="100%" align="center"><font size="+5" color="#000000"><H1>Ratings Report</H1></font></td>
</tr>
</table>
<P></P>
<form name="report_results" method="post" action="ExportAddToPortfolio">
<xsl:apply-templates select="/root/total_row_count"/>
<xsl:apply-templates select="/root/page_num"/>
</form>
</xsl:template>
<!-- End Main Template -->


<xsl:template match="total_row_count">

<xsl:variable name="page_no" select="/root/page_num/@num"/>
<xsl:variable name="start_row_no" select="(number($page_no) - 1) * 100 + 1"/>
<xsl:variable name="end_row_no" select="number($page_no) * 100"/>
<xsl:variable name="total_file_cnt" select="/root/total_file_count"/>
<xsl:variable name="total_row_cnt" select="/root/total_row_count"/>
<xsl:variable name="report_type" select="/root/report_type"/>

<table width="980" cellpadding="1" cellspacing="1" border="0" bgcolor="#e5ecf9">

<xsl:choose>
<xsl:when test="number($total_row_cnt) &gt; number(0)">
<tr>
<td bgcolor="#e5ecf9" align="left"><font size="-1" color="#000000"></font></td>
<td bgcolor="#e5ecf9" align="left"><font size="-1" color="#000000">
Results <b><xsl:value-of select="$start_row_no"/> - <xsl:choose>
   <xsl:when test="number($page_no)=number($total_file_cnt)">
      <xsl:value-of select="$total_row_cnt"/>
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select="$end_row_no"/>
   </xsl:otherwise>
   </xsl:choose> of <xsl:value-of select="."/></b></font></td>
</tr>
</xsl:when>
<xsl:otherwise>
    <tr width="100%" align="center"><H2>No result for this query.</H2></tr>
</xsl:otherwise>
</xsl:choose>

</table>
</xsl:template>



<xsl:template match="page_num">
<table width="100%" cellpadding="1" cellspacing="1" border="0">
<xsl:apply-templates select="./row"/>
</table>

<xsl:variable name="page_no" select="./@num"/>
<xsl:variable name="total_file_cnt" select="/root/total_file_count"/>
<xsl:variable name="report_id" select="/root/report_id"/>
<xsl:variable name="report_type" select="/root/report_type"/>
<xsl:variable name="total_row_cnt" select="/root/total_row_count"/>

<xsl:if test="number($total_row_cnt) &gt; number(0)">
<table width="980" cellpadding="1" cellspacing="1" border="0" bgcolor="#e5ecf9">
<tr>
<td bgcolor="#e5ecf9" width="8%" align="left"><font size="-1" color="#000000">Result Page</font>&nbsp;</td>

 <xsl:call-template name="loop">
  <xsl:with-param name="start" select="number(1)"/>
  <xsl:with-param name="repeat" select="number($total_file_cnt)"/>
  <xsl:with-param name="report_id" select="number($report_id)"/>
 </xsl:call-template>

<td bgcolor="#e5ecf9" width="60%" align="left">&nbsp;</td>
</tr>
</table>
<P></P>
<P></P>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="48%">
<!-- Begin Add to Portfolio -->
      <table border="0" cellspacing="0" cellpadding="0" width="100%">
            <tr>
                  <td>
                  <img src="/img/box_bl.gif" width="17" height="11" border="0" alt=""/><font xsl:use-attribute-sets="section_font"><b>Add To Portfolio</b></font></td>
            </tr>
                  <!--tr>
                        <td colspan="1" >
                              <xsl:call-template name="hline"/>
                        </td>
                  </tr-->
            <tr>
                  <td><font xsl:use-attribute-sets="text_font">Choose a portfolio from this pull-down list</font></td>
            </tr>
            <tr>
                  <td>
                        <input type="hidden" name="reportId"  value="{$report_id}"></input>
                        <input type="hidden" name="portfolioType"  value="{$report_type}"></input>
                        <input type="hidden" name="pagenum"  value="{$page_no}"></input>

                        <select name='PORTFOLIO' size='1'>
                                 <option value=".">Select Portfolio</option>
                                           <xsl:apply-templates select="./portfolio"/>
                        </select>

                        <br/>
                        <font xsl:use-attribute-sets='text_font'>or enter a name for a new portfolio</font>
                        <br/>
                        <input type="TEXT" name="PortfolioName" size="35" maxlength="32"></input>
                        <br/>
                        <input type="checkbox" name="chkbox" onclick ="myFunction();">Add All</input>
                        <br/>
                        <input type="submit" name="saveAsPort" value="AddPortfolio"></input>
                  </td>
            </tr>

      </table>
<!-- End Add to Portfolio -->
</td>
<td width="2%">
</td>
<td width="50%">
<!-- Begin Export -->
      <table border="0" cellspacing="0" cellpadding="0" width="100%">
            <tr>
                  <td><img src="/img/box_bl.gif" width="30" height="11" border="0" alt=""/><font xsl:use-attribute-sets="section_font"><b>Export Report</b></font></td>
            </tr>
            <tr>
                  <td colspan="2"><font xsl:use-attribute-sets="text_font">Please select an output type below</font></td>
            </tr>
            <tr>
                  <td width="10">
                        <select name="expFormat" size="1">
                              <option value="">Select Export Format</option>
                              <option value="Excel">Excel</option>
                              <option value="ASCII Text">ASCII Text</option>
                        </select>
                        <br/>
                  </td>
                  <td>
                        <input TYPE='button' name="btnExport" VALUE="Export" id="btnExport"
                              onClick='javascript:exportReport("/RRM/controller/ExcelReport?reportId={$report_id}&amp;type={$report_type}",
                              "/RRM/controller/ASCIIReport?reportId={$report_id}&amp;type={$report_type}");'></input>
                  </td>
            </tr>
      </table>
<!-- End Export -->
</td>
</tr>
</table>
</xsl:if>
</xsl:template>

<xsl:template name="loop">
 <xsl:param name="start">0</xsl:param>
 <xsl:param name="repeat"></xsl:param>
 <xsl:param name="report_id"></xsl:param>
 <xsl:if test="number($start) &lt;= number($repeat)">
<td bgcolor="#e5ecf9" width="2%" align="left"><font size="-1" color="#000000"><a href="/RRM/controller/HTMLReport?reportId={$report_id}&amp;pagenum={$start}"><xsl:value-of select="$start"/></a></font>&nbsp;</td>
  <xsl:call-template name="loop">
   <xsl:with-param name="start" select="$start + 1"/>
   <xsl:with-param name="repeat" select="number($repeat)"/>
   <xsl:with-param name="report_id" select="number($report_id)"/>
  </xsl:call-template>
 </xsl:if>
</xsl:template>




<xsl:template match="row">

<xsl:variable name="report_type" select="/root/report_type"/>
<xsl:variable name="curr_row_num" select="./@num"/>
<xsl:variable name="prev_row_num" select="number($curr_row_num) - number(1)"/>
<xsl:variable name="curr_entity_id" select="./col[@num='1']"/>
<xsl:variable name="prev_entity_id" select="//row[@num=$prev_row_num]/col[@num='1']"/>

<xsl:if test="(./@num mod 100)='1'">
<tr>
<td class="modcolhd">Add</td>
<xsl:for-each select="col">
<xsl:if test="@num != '1'"><td class="modcolhd"><xsl:value-of select="@name"/></td></xsl:if>
</xsl:for-each>
</tr>
</xsl:if>

<tr>
<xsl:variable name="rating_profile_url">RatingPage?entId=<xsl:value-of select="./col[@num='1']"/></xsl:variable>
<xsl:if test="$report_type='S'">
      <xsl:variable name="rating_profile_url">RatingPage?issueId=<xsl:value-of select="./col[@num='1']"/></xsl:variable>
</xsl:if>

<xsl:for-each select="./col">
   <xsl:choose>
   <xsl:when test="@num='2'">
      <td class="moddata2" nowrap="nowrap" height="5" colpadding="0" colspacing="0"><font size="-2">
      <xsl:if test="($curr_entity_id != $prev_entity_id) or (number($curr_row_num) mod 100) = number(1)">
            <input type="checkbox" name="entId" value="{$curr_entity_id}"></input>
      </xsl:if>
      </font></td>
      <td class="moddata2" nowrap="nowrap">
      <xsl:if test="($curr_entity_id != $prev_entity_id) or (number($curr_row_num) mod 100) = number(1)">
      <a href="{$rating_profile_url}"><xsl:value-of select="."/></a>
      </xsl:if>
      </td>
   </xsl:when>
   <xsl:otherwise>
      <xsl:if test="@num != '1'"><td class="moddata2" nowrap="nowrap"><xsl:value-of select="."/></td></xsl:if>
   </xsl:otherwise>
   </xsl:choose>
</xsl:for-each>
</tr>

</xsl:template>


<xsl:template match="portfolio">
  <xsl:for-each select="./row">
      <xsl:variable name="portfolio_id" select="./portfolio_id"/>
      <option value="{$portfolio_id}"><xsl:value-of select="./portfolio_name"/></option>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
0
archrajan
Asked:
archrajan
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Its a bit difficult from the XSL you posted as to where exactly you need to put this code.  So, here's a small example that shows you how its done, you should be able to easily adapt this to suit your needs.

XML:

    <xml>
        <node>Node 1</node>
        <node>Node 2</node>
        <node>Node 3</node>
        <node>Node 4</node>
        <node>Node 5</node>
    </xml>

XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="xml">
            <table>
                <xsl:for-each select="node">
                    <xsl:element name="tr">
                        <xsl:choose>
                            <xsl:when test="position() mod 2 = 0">
                                <xsl:attribute name="bgcolor">red</xsl:attribute>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:attribute name="bgcolor">yellow</xsl:attribute>
                            </xsl:otherwise>
                        </xsl:choose>
                        <xsl:value-of select="." />
                    </xsl:element>
                </xsl:for-each>
            </table>
        </xsl:template>
    </xsl:stylesheet>

Hope this helps.
0
 
dualsoulCommented:
i prefer a little trick to alternate colors - it can be easily extended to any number of colors.

here is it:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="xml">
       
        <!-- here we contain all alternate colors -->
        <xsl:variable name="colors">#00ff00,#ffff00</xsl:variable>
            <table>
                <xsl:for-each select="node">
                      <tr>
                          <td bgcolor="{substring($colors,(position() mod 2)*8+1,7)}"><xsl:value-of select="."/></td>
                      </tr>
               </xsl:for-each>
            </table>
        </xsl:template>
    </xsl:stylesheet>

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now