tags:

views:

343

answers:

2

I would like to create an HTML table with row colors changing based on position and content. But instead of alternating every row, I'd like to be able to group rows together, so that I can have some XML like this:

<itemlist>
   <item group="0">Conent...blah blah</item>
   <item group="0">Content...who cares</item>
   <item group="1">Content</item>
   <item group="2">Content</item>
   <item group="2">Content</item>
</itemlist>

And all of the items with group=0 are one color, and items with group=1 are another, and group=2 are either toggled back to the first color, or are their own color.

All I can seem to find out there is ways to alternate every row, but I can't seem to "get it" when it comes to actually using the node data to help me make the decision.

+1  A: 

The first two groups are simple as you can parse them based on their group number.

To handle group 2, consider using the preceding function to get a list of proir notes, and use count to determine how many there are. You can can then alternate values based on whether the count is even or odd.

OJ
Thanks for the tips. I am still digesting how I access the attribute data, and I will definitely check out those functions.
K. Brafford
+2  A: 

Here's an example of using "choose" to apply a different class value based on the group value. Something similar to this would work if you want to treat each group in a specific way. If your decision logic for handling group 2 is more complex, then you could place additional decision logic inside the "when" statement testing for group 2.

<xsl:template match="/">
    <ul>
        <xsl:apply-templates select="itemlist/item"/>
    </ul>
</xsl:template>

<xsl:template match="item">
    <li>
        <xsl:attribute name="class">
            <xsl:choose>
                <xsl:when test="@group = 0">
                    red
                </xsl:when>
                <xsl:when test="@group = 1">
                    green
                </xsl:when>
                <xsl:when test="@group = 2">
                    blue
                </xsl:when>
                <xsl:otherwise>
                    black
                </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:value-of select="."/>
    </li>
</xsl:template>

jmcdowell
Thanks! I was able to get my code to work based on your answer. Not only that, but I was also able to "get it" as far as accessing entity attributes where I was previously cloudy.
K. Brafford