|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: variable change in xsl:for-each
jdgarrett@xxxxxxxxxx wrote:
> The issue may occur where you might want to skip a row based upon
> another criteria rather than the fact that you current position is
> odd or even....
>
> <rows>
> <item position="1" visible="1">value</item>
> <item position="2" visible="0">value</item>
> <item position="3" visible="1">value</item>
> <item position="4" visible="1">value</item>
> <item position="5" visible="1">value</item>
> <item position="6" visible="0">value</item>
> </rows>
>
> if you then used position in the document to indicate what color a
> row would be then the code below would not work as desired...
True.
> so the solution is to call a function to set your variable
I guess you could, but it's not a very good idea in XSLT, since one of
the fundamental features of XSLT is that the order in which nodes are
actually processed doesn't matter -- a processor could perform the
transformation in reverse and it shouldn't change the result.
So in XSLT it's easier and better to simply select the nodes that you
want to show and use position() as normal:
<xsl:for-each select="item[@visible = 1]">
<xsl:variable name="RowColor">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">#FFFFFF</xsl:when>
<xsl:otherwise>#FFFF11</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr><td bgcolor="{$RowColor}"><xsl:value-of select="."></td></tr>
</xsl:for-each>
If the algorithm for selecting the nodes is very complex, such that
you can't get hold of a node set containing the items that you want to
include in the rows, then a recursive solution is better:
<xsl:template name="createRows">
<xsl:param name="rows" select="/rows/item" />
<xsl:param name="RowColor" select="'#FFFFFF'" />
<xsl:choose>
<xsl:when test="$rows[1]/@visible = 1">
<tr><td bgcolor="{$RowColor}"><xsl:value-of select="."></td></tr>
<xsl:call-template name="createRows">
<xsl:with-param name="rows" select="$rows[position() > 1]" />
<xsl:with-param name="RowColor">
<xsl:choose>
<xsl:when test="$RowColor = '#FFFFFF'">#FFFF11</xsl:when>
<xsl:otherwise>#FFFFFF</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$rows[2]">
<xsl:call-template name="createRows">
<xsl:with-param name="rows" select="$rows[position() > 1]" />
<xsl:with-param name="RowColor" select="$RowColor" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








