how to generate striped table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alepac
    New Member
    • Feb 2008
    • 2

    how to generate striped table?

    Hi everybody!

    this is my first post here but I would like to start saying thank you 'cos even if I never wrote here I read so many useful posts!

    Here is the issue coming...

    I need to generate an html table for a prods catalogue starting from xml.

    So, it has been easy to get the data I need but it seems to be not that easy to style it up!

    I'll let the code speak for me now...

    [code=txt]
    <table>
    <xsl:for-each select="Fields/field">
    <xsl:if test="not(conta ins(pName, 'blabla') or pValue = '' or @hiddenField='t rue')">
    <tr>
    <td class="label">
    <xsl:text> • </xsl:text>
    <xsl:value-of select="pName"/>
    </td>
    <td class="value">
    <xsl:value-of select="pValue"/>
    </td>
    </tr>
    </xsl:if>
    </xsl:for-each>
    </table>
    [/code]

    so the DREAM is to have something like a counter to find out if a row is odd or even and then to give it a class (like tr class=evenrow ..)

    Of course the first idea I had was to check the value of position() of a node but since I dont get all the nodes (because of the if condition) the value i get it's useless...

    Does anybody know some trick or just some easy solution for this?
    I know it probably can be a really stupid question for you but I'm a newbie with xsl and xpath...I'm founding it really exciting and in few days i learnt a lot but...not enough to solve this :)

    thanks in advance for any help!

    Ale
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You could have something like
    [code=xml]
    <xsl:variable name="position" >
    <xsl:choose>
    <xsl:when test="position( ) mod 2 = 0">evenrow</xsl:when>
    <xsl:otherwise> oddrow</xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <tr class="{$positi on}"/>
    [/code]
    Note, use of braces {} in the attribute that tells the processor that this is an xpath to be evaluated as opposed to a string.

    If you want this only on your filtered rows, you have to move the if test to the for-each like so:
    [code=xml]
    <xsl:for-each select="Fields/field[not(contains(pN ame, 'blabla') or pValue = '' or @hiddenField='t rue')]">
    [/code]
    and get rid of the if element.

    Comment

    • alepac
      New Member
      • Feb 2008
      • 2

      #3
      you cant imagine how stupid (and grateful to you) i feel!

      i had the key! i could get the position to filter my table elems but...i was not filtering them all together in the for-each! that's it!!! there was no reason to have a for-each and then a xsl if after! having them together i get just the elements I want and THEN i can use the position element! I obviously need to learn a little more xpath stuff :D

      I was looking for a really difficult solution for a really difficult problem when I could make the problem....real ly easy! Thanks for the help! thanks a lot!!! :)



      and...thank you again!!! :)

      Comment

      Working...