Rotate background color without using for-each

后端 未结 3 724
遇见更好的自我
遇见更好的自我 2021-01-24 19:38

I am trying to set the background color of every other I am not using a for-each loop since I am using a few templates to filter data based on the nodes

相关标签:
3条回答
  • 2021-01-24 20:11

    Here is the classic XSLT solution:

    Replace:

        <xsl:apply-templates> 
            <xsl:sort select="time" data-type="number" order="descending"/> 
        </xsl:apply-templates> 
    

    with:

            <xsl:variable name="vrtfResult">
             <xsl:apply-templates>
               <xsl:sort select="time" data-type="number" order="descending"/>
             </xsl:apply-templates>
           </xsl:variable>
    
           <xsl:apply-templates select="ext:node-set($vrtfResult)/tr"/>
    

    Replace your <stylesheet> element with:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     xmlns:my="my:my"  extension-element-prefixes="ext my">
    
     <my:colors>
      <c>#ffff</c>
      <c>#cccc</c>
     </my:colors>
    
     <xsl:variable name="vColors"
          select="document('')/*/my:colors/*"/>
    

    Finally, add this template:

     <xsl:template match="tr">
      <xsl:variable name="vPos" select="position()"/>
     <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="bgcolor">
        <xsl:value-of select="$vColors[($vPos mod 2)+1]"/>
      </xsl:attribute>
    
      <xsl:copy-of select="node()"/>
     </xsl:copy>
     </xsl:template>
    

    The complete XSLT code becomes:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     xmlns:my="my:my"  extension-element-prefixes="ext my">
    
     <my:colors>
      <c>#ffff</c>
      <c>#cccc</c>
     </my:colors>
    
     <xsl:variable name="vColors"
          select="document('')/*/my:colors/*"/>
    
     <xsl:template match="NewDataSet">
           <html>
                <body>
                 <table width="390" style="text-align:left;">
                              <tr>
                                <th style="text-align:left;"><span style="font:20px arial;
    font-weight:bold;">Agent Name</span></th>
                                <th style="text-align:center;"><span style="font:20px arial;
    font-weight:bold;">State</span></th>
                                <th style="text-align:center;" ><span style="font:20px arial;
    font-weight:bold;">Time</span></th>
                               </tr>
    
                    <xsl:variable name="vrtfResult">
                     <xsl:apply-templates>
                       <xsl:sort select="time" data-type="number" order="descending"/>
                     </xsl:apply-templates>
                   </xsl:variable>
    
                   <xsl:apply-templates select="ext:node-set($vrtfResult)/tr"/>
                 </table>
               </body>
            </html>
     </xsl:template>
    
     <xsl:template match="tr">
      <xsl:variable name="vPos" select="position()"/>
     <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="bgcolor">
        <xsl:value-of select="$vColors[($vPos mod 2)+1]"/>
      </xsl:attribute>
    
      <xsl:copy-of select="node()"/>
     </xsl:copy>
     </xsl:template>
    
     <xsl:template match="AgentSales[State='Talking Out']">
       <tr>
          <xsl:apply-templates/>
       </tr>
     </xsl:template>
    
           <xsl:template match="AgentSales/AgentName">
               <td style="text-align:left;">
                   <span style="font:14px arial;
    font-weight:bold;text-align:center;"> <xsl:value-of
    select="."/></span>
               </td>
    
           </xsl:template>
    
           <xsl:template match="AgentSales/State">
               <td style="text-align:center;">
                   <span style="font:14px arial;
    font-weight:bold;text-align:center;"> <xsl:value-of
    select="."/></span>
               </td>
    
           </xsl:template>
    
           <xsl:template match="AgentSales/time">
               <td style="text-align:center;">
                   <span style="font:14px arial;
    font-weight:bold;text-align:center;"> <xsl:value-of
    select="."/></span>
               </td>
    
           </xsl:template>
    
      <xsl:template match="AgentSales/Reason | AgentSales"/>
    </xsl:stylesheet>
    

    When applied on the provided XML document:

    <NewDataSet>
      <AgentSales>
        <AgentName>MCCALLISTER AARON</AgentName>
        <State>Talking Out</State>
        <Reason />
        <time>9</time>
      </AgentSales>
      <AgentSales>
        <AgentName>APPELHANS BARRY</AgentName>
        <State>Talking Out</State>
        <Reason />
        <time>1</time>
      </AgentSales>
      <AgentSales>
        <AgentName>ARREDONDO KARLA</AgentName>
        <State>Talking Out</State>
        <Reason />
        <time>0</time>
      </AgentSales>
    </NewDataSet>
    

    the wanted, correct result is produced:

    <html>
       <body>
          <table width="390" style="text-align:left;">
             <tr>
                <th style="text-align:left;"><span style="font:20px arial; font-weight:bold;">Agent Name</span></th>
                <th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">State</span></th>
                <th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">Time</span></th>
             </tr>
             <tr bgcolor="#cccc">
    
                <td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">MCCALLISTER AARON</span></td>
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
    
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">9</span></td>
    
             </tr>
             <tr bgcolor="#ffff">
    
                <td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">APPELHANS BARRY</span></td>
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
    
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">1</span></td>
    
             </tr>
             <tr bgcolor="#cccc">
    
                <td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">ARREDONDO KARLA</span></td>
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
    
    
                <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">0</span></td>
    
             </tr>
          </table>
       </body>
    </html>
    
    0 讨论(0)
  • 2021-01-24 20:16

    You could do this in CSS by using :nth-child([even/odd]) :

    tr:nth-child(even) td {
        background-color: #fff;
    }
    tr:nth-child(odd) td {
        background-color: #ccc;
    }
    

    However, this will not work on older browsers.

    0 讨论(0)
  • 2021-01-24 20:22

    I like CSS solutions for style, but for general questions about this subject and without extension function, you would need to use a push style like (Dimitre's stylesheet borrowed):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my"
     exclude-result-prefixes="my">
        <my:colors>
            <c>#ffff</c>
            <c>#cccc</c>
        </my:colors>
        <xsl:variable name="vColors" select="document('')/*/my:colors/*"/>
        <xsl:template match="NewDataSet">
            <html>
                <body>
                    <table width="390" style="text-align:left;">
                        <tr>
                            <th style="text-align:left;">
                                <span style="font:20px arial; font-weight:bold;"
                                 >Agent Name</span>
                            </th>
                            <th style="text-align:center;">
                                <span style="font:20px arial; font-weight:bold;"
                                 >State</span>
                            </th>
                            <th style="text-align:center;" >
                                <span style="font:20px arial; font-weight:bold;"
                                 >Time</span>
                            </th>
                        </tr>
                        <xsl:apply-templates 
                             select="AgentSales[State='Talking Out']">
                            <xsl:sort select="time" 
                                      data-type="number"
                                      order="descending"/>
                        </xsl:apply-templates>
                    </table>
                </body>
            </html>
        </xsl:template>
        <xsl:template match="AgentSales">
            <xsl:variable name="vPos" select="position()"/>
            <tr bgcolor="{$vColors[($vPos mod 2)+1]}">
                <xsl:apply-templates/>
            </tr>
        </xsl:template>
        <xsl:template match="AgentSales/*">
            <xsl:variable name="vAlign"
                          select="concat(substring('left',
                                                   1 div (self::AgentName)),
                                         substring('right',
                                                   1 div not(self::AgentName)))"/>
            <td style="text-align:{$vAlign};">
                <span style="font:14px arial; font-weight:bold;text-align:center;">
                    <xsl:value-of select="."/>
                </span>
            </td>
        </xsl:template>
        <xsl:template match="AgentSales/Reason"/>
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题