2010-07-15 16 views
8

Sono una tabella HTML scritto utilizzando XSLT trasformazione che assomiglia a questoXslt come stile condizionali pari/dispari righe

<table> 
    <xsl:for-each select="someNode"> 
     <xsl:if test="testThis"> 
      <tr> 
       <!-- <xsl:call-template name="conditionalRowStyle"/> --> 
       <td>something</td> 
      </tr> 
     </xsl:if> 
     <tr> 
      <!-- <xsl:call-template name="conditionalRowStyle"/> --> 
      <td>this is always displayed</td> 
     </tr> 
     <xsl:if test="testThis2"> 
      <tr> 
       <!-- <xsl:call-template name="conditionalRowStyle"/> --> 
       <td>something 2</td> 
      </tr> 
     </xsl:if> 
     .... 
    </xsl:for-each> 
    <tr> 
     <!-- <xsl:call-template name="conditionalRowStyle"/> --> 
     <td>this is always displayed</td> 
    </tr> 
</table> 

Ho bisogno di un modo per applicare diverse classi oddRow/evenRow a TR elems.

<tr class="evenRow"> or <tr class="oddRow"> 

ho cercato di utilizzare un modello come questo dopo ogni <tr> elem

<xsl:template name="conditionalRowStyle"> 
    <xsl:attribute name="class"> 
     <xsl:choose> 
      <xsl:when test="(count(../preceding-sibling::tr) mod 2) = 0">oddrow</xsl:when> 
      <xsl:otherwise>evenrow</xsl:otherwise> 
     </xsl:choose> 
    </xsl:attribute> 
</xsl:template> 

ma questo non sta funzionando. qualche idea?

+0

Ottima domanda (+1). Vedere la mia risposta per una soluzione XSLT 1.0 a passaggio singolo :) –

risposta

17

Probabilmente si potrebbe ottenere via con fare questo in just css

tr:nth-child(odd) { 
    /*...*/ 
} 
tr:nth-child(odd) { 
    /*...*/ 
} 

Se non è possibile, si potrebbe fare qualcosa di simile

<xsl:attribute name="class"> 
    <xsl:choose> 
     <xsl:when test="(position() mod 2) != 1"> 
      <xsl:text>evenRow</xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:text>oddRow</xsl:text> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:attribute> 

Nota che ho scritto questo nella casella di testo SO e non hanno testato.

+0

Questo non funzionerà, poiché i nodi selezionati sono i dati di origine utilizzati per creare la tabella, non la tabella stessa. – mdma

+0

Grazie per il tuo resp. Le tue soluzioni non funzioneranno. Non posso usare le proprietà CSS come in altre tabelle, ho bisogno di stampare a livello di codice più di 2 righe dello stesso colore. test = "(position() mod 2)! = 1 interrompe quando uso se le istruzioni mostrano righe condizionali – mickthompson

0

Sembra che il modello conditionalRowStyle per aggiungere stili alla tabella sia nello stesso foglio di stile di quello che crea la tabella. Se questo è il caso, allora non funzionerà come previsto, dal momento che i nodi selezionati nel modello conditionalRowStyle saranno dal documento di origine (contenente someNode) e non il documento di destinazione (dove sono gli elementi di tabella generati)

Tu può "hackerare" questo raccogliendo l'output della tabella dei modelli someNode su una variabile per primo, che è quindi possibile eseguire il modello conditionalRowStyle prima di emettere infine il valore della variabile come risultato del foglio di stile. Ma è molto più semplice usare due fogli di stile, che si eseguono uno dopo l'altro in una pipeline. Il primo foglio di stile converte i dati someNode in una tabella e il secondo applica la formattazione conditionalRowStyle alla tabella.

3

Questa trasformazione:

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

<xsl:variable name="vrtfTrs"> 
    <tr> 
    <td>something</td> 
    </tr> 
    <tr> 
    <td>This is always displayed</td> 
    </tr> 
    <tr> 
    <td>something 2</td> 
    </tr> 
</xsl:variable> 

<xsl:variable name="vTrs" select="ext:node-set($vrtfTrs)/*"/> 

<xsl:template match="/nums"> 
    <html> 
    <table> 
     <xsl:apply-templates select="num[1]"/> 
    </table> 
    </html> 
</xsl:template> 

<xsl:template match="num"> 
    <xsl:param name="pCount" select="0"/> 

    <xsl:variable name="vTest1" select=". mod 4 = 1"/> 
    <xsl:variable name="vTest2" select=". mod 4 = 2"/> 

    <xsl:apply-templates select="$vTrs[1][$vTest1]"> 
    <xsl:with-param name="pCount" select="$pCount +1"/> 
    <xsl:with-param name="pnodevalue" select="."/> 
    </xsl:apply-templates> 

    <xsl:apply-templates select="$vTrs[2]"> 
    <xsl:with-param name="pCount" select="$pCount+$vTest1 +1"/> 
    <xsl:with-param name="pnodevalue" select="."/> 
    </xsl:apply-templates> 

    <xsl:apply-templates select="$vTrs[3][$vTest2]"> 
    <xsl:with-param name="pCount" select="$pCount+$vTest1 +2"/> 
    <xsl:with-param name="pnodevalue" select="."/> 
    </xsl:apply-templates> 

    <xsl:apply-templates select="following-sibling::*[1]"> 
    <xsl:with-param name="pCount" 
      select="$pCount+1+$vTest1+$vTest2"/> 
    <xsl:with-param name="pnodevalue" select="."/> 
    </xsl:apply-templates> 

    <xsl:if test="not(following-sibling::*)"> 
     <xsl:apply-templates select="$vTrs[2]"> 
     <xsl:with-param name="pCount" select="$pCount+1+$vTest1+$vTest2"/> 
     <xsl:with-param name="pnodevalue" select="."/> 
     </xsl:apply-templates> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="tr"> 
    <xsl:param name="pCount"/> 
    <xsl:param name="pnodevalue"/> 

    <tr class="{concat(substring('even', 1 div ($pCount mod 2 = 0)), 
        substring('odd', 1 div ($pCount mod 2 = 1)) 
        )}"> 
    <xsl:comment>&lt;num><xsl:value-of select="$pnodevalue"/>&lt;/num></xsl:comment> 
    <xsl:copy-of select="node()"/> 
    </tr> 
</xsl:template> 
</xsl:stylesheet> 

se applicato su questo documento XML:

<nums> 
    <num>01</num> 
    <num>02</num> 
    <num>03</num> 
    <num>04</num> 
    <num>05</num> 
    <num>06</num> 
    <num>07</num> 
    <num>08</num> 
    <num>09</num> 
    <num>10</num> 
</nums> 

produce il risultato voluto:

<html> 
    <table> 
     <tr class="odd"> 
     <!--<num>01</num>--> 
     <td>something</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>01</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>02</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>02</num>--> 
     <td>something 2</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>03</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>04</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>05</num>--> 
     <td>something</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>05</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>06</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>06</num>--> 
     <td>something 2</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>07</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>08</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>09</num>--> 
     <td>something</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>09</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="odd"> 
     <!--<num>10</num>--> 
     <td>This is always displayed</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>10</num>--> 
     <td>something 2</td> 
     </tr> 
     <tr class="even"> 
     <!--<num>10</num>--> 
     <td>This is always displayed</td> 
     </tr> 
    </table> 
</html> 

Do atto:

  1. Stiamo usando l'attraversamento più a grana fine e l'elaborazione di un documento XML - il nodo per nodo. Dopo la trasformazione dell'identità, questo è il secondo modello di progettazione XSLT più importante.

  2. Il resto dei piccoli accorgimenti non è così importante.

+0

+1 Eccellente! Rompendo la ricorsione e procedendo in sequenza con l'asse seguente ... –

+0

@Alejandro: Sì, dobbiamo ricordare che a volte '' è troppo grossolano –

+0

scegliendo l'odd, anche l'accesso è fantastico – kalyan

Problemi correlati