开发者

different colors for each row

Is it possible to give a different color to each row that has distinct data, using xslt ?

For example, in case there is a table of the form

country        code
india           1
spain           2
germany         3
india           1
sri lanka       4
spain           2

There are 2 rows in which india and spain occur. so, can I color those 2 rows with a particular color and the remaining with different colors? Suppose sri lanka occurs twice, I wish sri lanka rows to have a different color. dis can b done using xslt?

The xml file gets updated dynamically. The xsl reads from the xml file and displays the data in the form of a table. 2 tables will be outputed. I want that, in the 2nd table, each row with distinct 'Conference name' should have a different color.

my xsl file

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">

    <html>
      <head>
        <title>VPGate Media Mixer</title>

        <meta http-equiv="expires" content="0"/>
        <meta http-equiv="pragma" content="no-cache"/>
        <meta http-equiv="cache-control" content="no-cache, must-revalidate"/>
        <meta http-equiv="refresh" content="15"></meta>
        <script src="/Common/common.js\" type="text/javascript"></script>
        <link rel="stylesheet" type="text/css" href="style001.css" />
        <link rel="stylesheet" type="text/css" href="Grid.Default.css" />


      </head>

      <body class="WorkArea">
        <div class="divSummaryHeader" id="SummaryHeader">
          <h1>Media Mixer - VPGate</h1>

          <xsl:for-each select="MMDiagnostics/Conference">
            <h1>
              Media Mixer - <xsl:value-of select="name"/>
            </h1>
          </xsl:for-each>
        </div>

        &#160;



        <div class="RadGrid RadGrid_Default" id="SummaryData" style="position:absolute;width:810px;height:510px;overflow:auto">

          <table border="0" class="rgMasterTable rgClipCells" cellspacing="0" cellpadding="0" >
            <tr>
              <input type="button" class="formEditBtn" id="SubBtn" value="Refresh" onclick="window.location=window.location;"/>
开发者_Python百科
            </tr>
            <tr>
              <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;" colspan="2">Conference Summary</td>
            </tr>

            <tr>
              <td>
                <table border="0" class="rgMasterTable rgClipCells" cellspacing="0" cellpadding="0"  >
                  <tr>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Conference Name</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Conference ID</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Composite Address</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Composite Port</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Composite Ssrc</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">No Of Participants</td>
                  </tr>

                  <xsl:if test="MediaMixer!= ''">
                    <xsl:for-each select="MediaMixer/Conference">
                      <!--<xsl:sort select="Name"/>-->
                      <xsl:if test="Name !=''">
                        <xsl:if test="(position() mod 2 = 0)">

                          <tr class="rgAltRow SummaryTableDataRow">
                            <td valign = "top">
                              <xsl:value-of select="Name"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="ConfId"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="CompositeAddress"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="CompositePort"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="CompositeSsrc"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="NoOfParticipants"/>
                            </td>
                          </tr>
                        </xsl:if>


                        <xsl:if test="(position() mod 2 = 1)">
                          <td>
                            <tr class="rgRow SummaryTableDataRow">
                              <td valign = "top">
                                <xsl:value-of select="Name"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="ConfId"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="CompositeAddress"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="CompositePort"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="CompositeSsrc"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="NoOfParticipants"/>
                              </td>
                            </tr>
                          </td>
                        </xsl:if>
                      </xsl:if>
                    </xsl:for-each>
                  </xsl:if>
                  <xsl:if test="MediaMixer = ''">
                    <td valign = "top">
                      <xsl:text>No Data </xsl:text>
                    </td>
                  </xsl:if>

                </table>
              </td>
            </tr>
          </table>

          &#160;

          <table border="0" class="rgMasterTable rgClipCells" cellspacing="1" cellpadding="1"  >
            <tr>
              <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;" colspan="2">Conference Details</td>
            </tr>

            <tr>
              <td>

                <table border="0" class="rgMasterTable rgClipCells" cellspacing="0" cellpadding="0" >
                  <tr>

                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Conference Name</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Conference ID</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Participant ID 1</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Participant ID 2</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Participant Address</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">Participant Listening Port</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">MM Listening Port</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">SSRC From Participant</td>
                    <td class="rgHeader SummaryTableHdrRow" style="font-weight:bold;">SSRC From MM</td>
                  </tr>





                  <xsl:if test="MediaMixer!= ''">

                    <xsl:for-each select="MediaMixer/Conference">


                      <xsl:for-each  select="Participant">

                        <xsl:if test="(position() mod 2 = 0)">
                          <tr class="rgAltRow SummaryTableDataRow">
                            <td valign = "top">
                              <xsl:value-of select="../Name"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="../ConfId"/>
                            </td>

                            <td valign = "top">
                              <xsl:value-of select="ID1"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="ID2"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="ParticipantAddress"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="ParticipantListeningPort"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="MMListeningPort"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="SSRCFromParticipant"/>
                            </td>
                            <td valign = "top">
                              <xsl:value-of select="SSRCFromMM"/>
                            </td>
                          </tr>
                        </xsl:if>

                        <xsl:if test="(position() mod 2 = 1)">
                          <td>
                            <tr class="rgRow SummaryTableDataRow">

                              <td valign = "top">
                                <xsl:value-of select="../Name"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="../ConfId"/>
                              </td>

                              <td valign = "top">
                                <xsl:value-of select="ID1"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="ID2"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="ParticipantAddress"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="ParticipantListeningPort"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="MMListeningPort"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="SSRCFromParticipant"/>
                              </td>
                              <td valign = "top">
                                <xsl:value-of select="SSRCFromMM"/>
                              </td>

                            </tr>
                          </td>
                        </xsl:if>
                      </xsl:for-each>
                    </xsl:for-each>
                  </xsl:if>


                  <xsl:if test="MediaMixer= ''">

                    <td valign = "top">
                      <xsl:text>No Data </xsl:text>
                    </td>
                  </xsl:if>

                </table>
              </td>
            </tr>
          </table>


          &#160;


          <div style="display:none">
            <iframe id="frameUpdate" name="frameUpdate" width="100%"></iframe>
          </div>


        </div>

      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

XML file that gets updated dynamically

<?xml-stylesheet type="text/xsl" href="MMDiagnostics.xslt"?>

<MediaMixer>

  <Conference>
    <Name>Test</Name>
    <ConfId>1002</ConfId>
    <CompositeAddress>238.57.0.1</CompositeAddress>
    <CompositePort>48000</CompositePort>
    <CompositeSsrc>243324353</CompositeSsrc>
    <NoOfParticipants>2</NoOfParticipants>

    <Participant>
      <ID1>abc88C</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>

    <Participant>
      <ID1>piy65R</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>   

  </Conference>

  <Conference>
    <Name>Test3</Name>
    <ConfId>1007</ConfId>
    <CompositeAddress>238.57.0.1</CompositeAddress>
    <CompositePort>48000</CompositePort>
    <CompositeSsrc>243324353</CompositeSsrc>
    <NoOfParticipants>2</NoOfParticipants>

    <Participant>
      <ID1>abxxC</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>

    <Participant>
      <ID1>yyy65R</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>

  </Conference>

  <Conference>
    <Name>Test002</Name>
    <ConfId>1002</ConfId>
    <CompositeAddress>238.57.0.1</CompositeAddress>
    <CompositePort>48005</CompositePort>
    <CompositeSsrc>353324353</CompositeSsrc>
    <NoOfParticipants>2</NoOfParticipants>

    <Participant>
      <ID1>70542151</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>

    <Participant>
      <ID1>70542151</ID1>
      <ID2>0</ID2>
      <ParticipantAddress>192.168.177.45</ParticipantAddress>
      <ParticipantListeningPort>22004</ParticipantListeningPort>
      <MMListeningPort>45004</MMListeningPort>
      <SSRCFromParticipant>316541</SSRCFromParticipant>
      <SSRCFromMM>26481</SSRCFromMM>
    </Participant>
  </Conference>


  </MediaMixer>

output looks like this as of now

different colors for each row


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kCountryByName" match="country"
  use="@name"/>

 <my:colors>
  <color bgcolor="aqua"/>
  <color bgcolor="blue"/>
  <color bgcolor="fuchsia"/>
  <color bgcolor="gray"/>
  <color bgcolor="green"/>
  <color bgcolor="lime"/>
  <color bgcolor="maroon"/>
  <color bgcolor="navy"/>
  <color bgcolor="purple"/>
  <color bgcolor="yellow"/>
 </my:colors>

 <xsl:variable name="vColors"
      select="document('')/*/my:colors/*"/>

 <xsl:variable name="vDistinctCountries" select=
  "/*/country
      [generate-id()
      =
       generate-id(key('kCountryByName', @name)[1])
      ]"/>

 <xsl:template match="/*">
  <html>
   <table>
    <xsl:apply-templates/>
   </table>
  </html>
 </xsl:template>

 <xsl:template match="country">
  <xsl:variable name="vthisName" select="@name"/>

  <xsl:variable name="vNum">
   <xsl:for-each select="$vDistinctCountries">
    <xsl:if test="@name = $vthisName">
     <xsl:value-of select="position()"/>
    </xsl:if>
   </xsl:for-each>
  </xsl:variable>

  <tr bgcolor="{$vColors[position()=$vNum]/@bgcolor}">
    <td><xsl:value-of select="@name"/></td>
    <td><xsl:value-of select="@continent"/></td>
  </tr>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (as no XML document was provided!!!):

<data>
 <country name="india" continent="Asia"/>
 <country name="spain" continent="Europe"/>
 <country name="germany" continent="Europe"/>
 <country name="india" continent="Asia"/>
 <country name="sri lanka" continent="Asia"/>
 <country name="spain" continent="Europe"/>
</data>

produces the wanted, correct result:

<html>
   <table>
      <tr bgcolor="aqua">
         <td>india</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="blue">
         <td>spain</td>
         <td>Europe</td>
      </tr>
      <tr bgcolor="fuchsia">
         <td>germany</td>
         <td>Europe</td>
      </tr>
      <tr bgcolor="aqua">
         <td>india</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="gray">
         <td>sri lanka</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="blue">
         <td>spain</td>
         <td>Europe</td>
      </tr>
   </table>
</html>

Explanation: Muenchian grouping.

UPDATE: If there isn't a known upper limit for the number of distinct countries, use something like this:

<tr bgcolor="#{$vNum*123456 mod 16777216}">

The complete transformation becomes:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kCountryByName" match="country"
  use="@name"/>

 <my:colors>
  <color bgcolor="aqua"/>
  <color bgcolor="blue"/>
  <color bgcolor="fuchsia"/>
  <color bgcolor="gray"/>
  <color bgcolor="green"/>
  <color bgcolor="lime"/>
  <color bgcolor="maroon"/>
  <color bgcolor="navy"/>
  <color bgcolor="purple"/>
  <color bgcolor="yellow"/>
 </my:colors>

 <xsl:variable name="vColors"
      select="document('')/*/my:colors/*"/>

 <xsl:variable name="vDistinctCountries" select=
  "/*/country
      [generate-id()
      =
       generate-id(key('kCountryByName', @name)[1])
      ]"/>

 <xsl:template match="/*">
  <html>
   <table>
    <xsl:apply-templates/>
   </table>
  </html>
 </xsl:template>

 <xsl:template match="country">
  <xsl:variable name="vthisName" select="@name"/>

  <xsl:variable name="vNum">
   <xsl:for-each select="$vDistinctCountries">
    <xsl:if test="@name = $vthisName">
     <xsl:value-of select="position()"/>
    </xsl:if>
   </xsl:for-each>
  </xsl:variable>

  <tr bgcolor="#{$vNum*1234567 mod 16777216}">
    <td><xsl:value-of select="@name"/></td>
    <td><xsl:value-of select="@continent"/></td>
  </tr>
 </xsl:template>
</xsl:stylesheet>

and the result now is:

<html>
   <table>
      <tr bgcolor="#1234567">
         <td>india</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="#2469134">
         <td>spain</td>
         <td>Europe</td>
      </tr>
      <tr bgcolor="#3703701">
         <td>germany</td>
         <td>Europe</td>
      </tr>
      <tr bgcolor="#1234567">
         <td>india</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="#4938268">
         <td>sri lanka</td>
         <td>Asia</td>
      </tr>
      <tr bgcolor="#2469134">
         <td>spain</td>
         <td>Europe</td>
      </tr>
   </table>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜