Re: XSLT for-each get previous item

Discussion in 'XML' started by Mukul Gandhi, Feb 6, 2009.

  1. Mukul Gandhi

    Mukul Gandhi Guest

    On Feb 6, 6:00 am, DustWolf <> wrote:
    >    <xsl:for-each select="/values/value">
    >     <item last="{/values/value[position()-1]}" current="{.}" />
    >    </xsl:for-each>
    >
    > ...unfortunately since position() always gives the position within the
    > current context, it returns the position within /values/value (which
    > is always 1), instead of my for-each loop.


    you should be able to do this by using a variable,

    <xsl:for-each select="/values/value">
    <xsl:variable name="pos" select="position()" />
    <item last="{/values/value[$pos-1]}" current="{.}" />
    </xsl:for-each>

    This is not tested though.
    Mukul Gandhi, Feb 6, 2009
    #1
    1. Advertising

  2. * DustWolf:

    > On 6 feb., 05:39, Mukul Gandhi <> wrote:
    >> you should be able to do this by using a variable,
    >>
    >> <xsl:for-each select="/values/value">
    >>   <xsl:variable name="pos" select="position()" />
    >> <item last="{/values/value[$pos-1]}" current="{.}" />
    >> </xsl:for-each>
    >>
    >> This is not tested though.

    >
    > It works (interestingly, as I found answers all over the web
    > specifically stating that this could not be done, e.g. that an XSLT
    > variable is actually a constant which's value cannot be changed once
    > defined),


    actually, $pos *is* a constant. It just goes out of scope
    at the end of each for-each iteration, and the next round's
    $pos is a new one.

    |   <xsl:variable name="pos" select="$pos + 1" />

    would not work, however.
    Thorsten Vitt, Feb 9, 2009
    #2
    1. Advertising

  3. Mukul Gandhi

    worlebird

    Joined:
    Oct 17, 2006
    Messages:
    2
    Still not getting previous item

    This doesn't work - this returns the previous node in the original XML, not the previous node from the for-each loop. Usually, these are the same, unless you are using a sort in the for-each loop. In this case, the for-each loop will return the nodes out of order. Position() will reflect this fact, but the XPath does not honor it. I need to know how to get the previous node returned by the for-each loop.

    Any help would be appreciated.
    worlebird, Feb 10, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  2. Johannes Zellner
    Replies:
    1
    Views:
    488
    Alex Martelli
    Jan 17, 2006
  3. Martin Honnen

    Re: XSLT for-each get previous item

    Martin Honnen, Feb 6, 2009, in forum: XML
    Replies:
    2
    Views:
    2,049
    Martin Honnen
    Feb 7, 2009
  4. KJ
    Replies:
    2
    Views:
    152
  5. Jari Williamsson

    each with previous?

    Jari Williamsson, Dec 14, 2007, in forum: Ruby
    Replies:
    6
    Views:
    95
    Jari Williamsson
    Dec 14, 2007
Loading...

Share This Page