2011-08-05 31 views
2

我使用xslt從xml創建表,並且我希望每個其他人都是不同的類。xslt中的斑馬行

這裏是XML:

<interfaces> 
    <interface id="250" name="112test" odd="1"></interface> 
    <interface id="251" name="113test" odd="0"></interface> 
</interfaces> 

這裏是XSLT的相關部分我已經試過:

<xsl:template match="interfaces"> 
    <xsl:for-each select="interface"> 
    <xsl:choose> 
     <xsl:when test="@odd = '1'"> 
     <tr class="odd"> 
     </xsl:when> 
     <xsl:otherwise> 
     <tr> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:for-each> 
</xsl:template> 

我如何得到這個工作?或者有沒有更好的方法來做到這一點,例如通過檢查屬性是否存在而不是檢查其值?

+0

http://www.java2s.com/Tutorial/XML/0100__XSLT-stylesheet/Changestyleforevenandodd.htm – arunkumar

+0

這是什麼這不符合你目前的做法?是否事實上你的''元素是未封閉的,你試圖避免重複內容邏輯?還是有一些你不喜歡的其他方面,比如源頭上多餘的「奇怪」屬性? – Tao

+0

''元素已關閉,但我刪除了任何不必要的代碼。這是我不喜歡的奇怪屬性。 –

回答

1

的完美解決方案最終是user639175的組合和馬修·威爾森的答案:

<xsl:if test="position() mod 2 = 1"> 
    <xsl:attribute name="class">odd</xsl:attribute> 
</xsl:if> 
2

未經測試,但類似下面的內容應該可以工作。

<tr> 
    <xsl:if test="@odd = '1'"> 
     <xsl:attribute name="class">odd</xsl:attribute> 
    </xsl:if> 
    ... other stuff here 
</tr> 
+0

工程很好,非常感謝! –

3
<xsl:choose> 
    <xsl:when test="position() mod 2 = 1">odd</xsl:when> 
    <xsl:otherwise>even</xsl:otherwise> 
</xsl:choose> 

Oscilliate奇/偶節點之間......

-1

救自己頭痛和使用JQuery或者你選擇的框架,如果有合適的選擇。

$(‘tr’:odd).addClass('stripe'); 

或CSS 3

tr:nth-child(odd) { background-color:#eee; } 

保持標記清潔。

+0

會很高興在這裏提及jQuery(或同等產品?),因爲您依賴於JS解決方案...... – Tao

+0

夠公平的!我有時候會假設太多...... – Emyr

+1

這是一個商業應用程序,所以我必須支持IE6 +(否則我會用CSS3解決方案),並且我沒有「安裝」jQuery的權限。 –