每行不同的颜色

时间:2011-09-27 03:25:06

标签: xslt

是否可以使用xslt?

为具有不同数据的每一行提供不同的颜色

例如,如果有表格

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

印度和西班牙有两排。那么,我可以用特定颜色为这两行着色,剩下的颜色是不同的颜色吗?假设斯里兰卡出现两次,我希望斯里兰卡行有不同的颜色。可以使用xslt完成吗?

xml文件动态更新。 xsl从xml文件读取并以表格形式显示数据。 2张桌子将被输出。我想要的是,在第二个表格中,每个具有不同“会议名称”的行应该有不同的颜色。

我的xsl文件

<?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;"/>

            </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文件

<?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>

输出看起来像现在这样

enter image description here

1 个答案:

答案 0 :(得分:0)

此转化

<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>

应用于此XML文档(因为没有提供XML文档!!!):

<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>

生成想要的正确结果

<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>

解释:Muenchian分组。

更新:如果不同国家/地区的数量没有已知上限,请使用以下内容:

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

完整转化

<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>

,结果现在是

<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>