This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Accessing multiple values of an attribute
- From: "Ben Robb" <Ben at cscape dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 1 Oct 2002 18:25:57 +0100
- Subject: RE: [xsl] Accessing multiple values of an attribute
- Reply-to: xsl-list at lists dot mulberrytech dot com
If you can possibly change your XML it would help:
<Table>
<stripes>
<stripe>blue</stripe>
<stripe>red</stripe>
etc
This would mean that you could do this:
====================================
<xsl:template match="Boxrow">
<xsl:variable name="colorSelect" select="((position() -1) mod
number($numStripes) + 1)"/>
<tr><td bgcolor="{/Table/stripes/stripe[$colorSelect]}">
<xsl:apply-templates />
</td></tr>
</xsl:template>
====================================
As you will see, this is a lot easier than the problem you have.
Apart from that (if you can't change it), you were on the right lines
when you were talking about using the xpath string functions to find the
total number for your mod and the values. There isn't really an easier
way, I'm afraid.
Firstly, the total number of colours is given by "$t", which is built
using a recursive function:
====================================
<xsl:variable name="t">
<xsl:call-template name="getTotal">
<xsl:with-param name="s"
select="normalize-space(/Table/@stripes)"/>
<xsl:with-param name="x" select="'1'"/>
</xsl:call-template>
</xsl:variable>
<xsl:template name="getTotal">
<xsl:param name="s"/>
<xsl:param name="x"/>
<xsl:choose>
<xsl:when test="contains($s, ' ')">
<xsl:call-template name="getTotal">
<xsl:with-param name="s"
select="substring-after($s,' ')"/>
<xsl:with-param name="x" select="$x +
1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of
select="$x"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
====================================
Secondly, to actually get the colour you want, you need to call another
recursive template like this (from within the Boxrow template):
====================================
<xsl:template match="Boxrow">
<xsl:variable name="mybgcolor">
<xsl:call-template name="getColor">
<xsl:with-param name="x" select="((position() -
1) mod number($t) )"/>
<xsl:with-param name="s"
select="normalize-space(/Table/@stripes)"/>
</xsl:call-template>
</xsl:variable>
<tr>
<td bgcolor="{normalize-space($mybgcolor)}">
<xsl:apply-templates />
</td></tr>
</xsl:template>
<xsl:template name="getColor">
<xsl:param name="x" />
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="number($x) > 0">
<xsl:call-template name="getColor">
<xsl:with-param name="x"
select="number($x) - 1"/>
<xsl:with-param name="s"
select="substring-after($s,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of
select="substring-before(concat($s, ' ') , ' ')"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
====================================
Hope this helps,
Ben
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list