2011-02-10 4 views
6

XSLドキュメントには、多数の記事が挿入されています。 HTML table with alternating row colors via XSLXSLを使用してtrクラスの行の色を交互に表示

をしかし、私の場合は、私は信じて異なっている: - 私は記事の背景色を交互にする必要があり、その後、「奇数」「偶数」

<xsl:for-each select="newsletter/section/article"> 
    <tr class="odd" style="background-color: #efefef;"> 
     <td valign="top"> 
      <xsl:element name="a"> 
       <xsl:attribute name="href"> 
        <xsl:value-of select="link" /> 
       </xsl:attribute> 
       <img align="left" valign="top" width="110" 
          style="padding: 0 4px 4px 0; border:0;"> 
        <xsl:attribute name="alt"> 
         <xsl:value-of select="title" /> 
        </xsl:attribute> 
        <xsl:attribute name="src"> 
         <xsl:value-of select="img" /> 
        </xsl:attribute> 
       </img> 
      </xsl:element> 
     </td> 
     <td valign="top" style="padding: 4px 4px 18px 0;"> 
      <strong> 
       <xsl:element name="a"> 
        <xsl:attribute name="href"> 
         <xsl:value-of select="link" /> 
        </xsl:attribute> 
        <xsl:value-of select="title"/> 
       </xsl:element> 
      </strong> 
      <br /> 
      <xsl:value-of select="excerpt"/> 
     </td> 
    </tr> 
</xsl:for-each> 

アイブ氏は、この記事を見ました。私は各反復でtrクラスを変更するだけです。奇妙な書式設定で申し訳ありませんが、ここにChromeでコードを貼り付ける際に問題が発生しているようです。

+0

あなたの要件はあなたにリンクした記事のものと異なっている方法の詳細を説明してもらえます。 –

+0

良い質問、+1。完全で短期的な解決策については私の答えを見てください。 :) –

+0

実際には[XSL経由で行の色が交互に切り替わるHTMLテーブル]の正確な複製です(http://stackoverflow.com/questions/469917/html-table-with-alternating-row-colors-via-xsl) –

答えて

8

用途:

<xsl:for-each select="newsletter/section/article"> 
    <xsl:variable name="vColor"> 
    <xsl:choose> 
     <xsl:when test="position() mod 2 = 1"> 
     <xsl:text>#efefef</xsl:text> 
     </xsl:when> 
     <xsl:otherwise>#ababab</xsl:otherwise> 
     </xsl:choose> 
     </xsl:variable> 


    <tr class="odd" style="background-color: {$vColor};"> 
+0

ありがとう。これで解決しました。リンクされた質問(私がそれを理解した後)を読んで、私の要件が似ていることが分かりました。答えに時間をとってくれてありがとう。 – BobFlemming

+0

@dimitre - '= 1'は必要ですか?私はちょうど行う場合、私は期待どおりの結果を得る: ' ます。 #efefefます。 #ababab 'です。これが悪い考えである理由はありますか? –

+0

@DevNull。私はそれがOKだと思います。単純に数値 - >ブール変換。 – Flack

0

position()を使用し、残りを2で割ったときに使用します。

0

あなたのケースは非常に似ています。あなたがすべきことは、位置に基づいてクラス名を定義することです。 @Jim Garrisonはあなたに良いプロンプトを与えますが、参考にしている例にposition()の例があり、質問が尋ねられるので、例が必要だと思います。

<xsl:for-each select="newsletter/section/article"> 
    <!-- create index-based variable --> 
    <xsl:variable name="classname"> 
    <xsl:choose> 
     <xsl:when test="position() mod 2 = 0"> 
     <xsl:text>even</xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:text>odd</xsl:text> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 
    <!-- insert contents of your variable --> 
    <tr class="{$classname}" style="background-color: #efefef;"> 
.... 
2
<xsl:for-each select="date"> 
    <tr> 
     <xsl:if test="position() mod 2 = 1"> 
      <xsl:attribute name="class">odd</xsl:attribute> 
      <xsl:attribute name="style">background-color: #efefef;" 
      </xsl:attribute> 
     </xsl:if> 
     <td valign="top"> 
      <a href="{link}"> 
       <img align="left" valign="top" width="110" 
          style="padding: 0 4px 4px 0; border:0;" 
          alt="{title}" 
          src="{img}"/> 
      </a> 
     </td> 
     <td valign="top" style="padding: 4px 4px 18px 0;"> 
      <strong> 
       <a href="{link}"> 
        <xsl:value-of select="title"/> 
       </a> 
      </strong> 
      <br /> 
      <xsl:value-of select="excerpt"/> 
     </td> 
    </tr> 
</xsl:for-each> 
+0

また、AVTでコードをクリーンアップする方法をOPを示すために+1。 –

関連する問題