[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: variable change in xsl:for-each

Subject: Re: variable change in xsl:for-each
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 22 Jul 2002 19:01:08 +0100
xsl fo variable
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...


> 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:when test="position() mod 2 = 1">#FFFFFF</xsl:when>
    <tr><td bgcolor="{$RowColor}"><xsl:value-of select="."></td></tr>

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: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:when test="$RowColor = '#FFFFFF'">#FFFF11</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" />


Jeni Tennison

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.