XSLT: Select nodes in reverse order

Discussion in 'XML' started by thrill5, May 14, 2006.

  1. thrill5

    thrill5 Guest

    I have an xml document such as:
    <device>
    <element>first</element>
    <element>second</element>
    </device>

    I am using this as the source for an xslt transform that goes like

    <xsl:for-each select="/device/element>
    This is the <xsl:value-of select="."/> element.
    </xsl:for-each>

    This would result in the following:

    This is the first element
    This is the second element

    Is it possible to select the nodes in reverse order so that I would end up
    with:

    This is the second element
    This is the first element

    I can add a unique attribute to <element> if that would help such as:
    <device>
    <element id="1">first</element>
    <element id="2">second</element>
    </device>

    Is there an XPath statement I can use in the <xsl:for-each> to select the
    elements in reverse order using the attribute as an order tag?

    Scott
    thrill5, May 14, 2006
    #1
    1. Advertising

  2. thrill5

    Jeff Higgins Guest

    Re: Select nodes in reverse order

    thrill5 wrote:
    >I have an xml document such as:
    > <device>
    > <element>first</element>
    > <element>second</element>
    > </device>
    >
    > I am using this as the source for an xslt transform that goes like
    >
    > <xsl:for-each select="/device/element>
    > This is the <xsl:value-of select="."/> element.
    > </xsl:for-each>
    >
    > This would result in the following:
    >
    > This is the first element
    > This is the second element
    >
    > Is it possible to select the nodes in reverse order so that I would end up
    > with:
    >
    > This is the second element
    > This is the first element
    >
    > I can add a unique attribute to <element> if that would help such as:
    > <device>
    > <element id="1">first</element>
    > <element id="2">second</element>
    > </device>
    >
    > Is there an XPath statement I can use in the <xsl:for-each> to select the
    > elements in reverse order using the attribute as an order tag?
    >
    > Scott
    >


    Scott,
    I'm real new at this so take it with a grain of salt.

    <xsl:template match="//device">
    <xsl:for-each select="element">
    <xsl:sort select="@id" data-type="number" order="descending"/>
    <xsl:text>This is the </xsl:text>
    <xsl:value-of select="."/>
    <xsl:text> element.</xsl:text>
    </xsl:for-each>
    </xsl:template>
    Jeff Higgins, May 14, 2006
    #2
    1. Advertising

  3. Re: Select nodes in reverse order

    Jeff Higgins wrote:
    > I'm real new at this so take it with a grain of salt.


    You were on the right track...

    The nodes selected by xsl:for-each are normally presented in document
    order. To get reverse document order, you want to reverse that
    positioning -- which can be done by re-sorting in descending order by
    original position.

    <xsl:template match="/">
    <xsl:for-each select="/device/element">
    <xsl:sort select="position()" data-type="number" order="descending"/>
    This is the <xsl:value-of select="."/> element.
    </xsl:for-each>
    </xsl:template>

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, May 14, 2006
    #3
  4. thrill5

    Jeff Higgins Guest

    Re: Select nodes in reverse order

    Joe Kesselman wrote:

    > ... To get reverse document order, you want to reverse that positioning --
    > which can be done by re-sorting in descending order by original position.
    >
    > <xsl:sort select="position()" data-type="number" order="descending"/>
    >


    How neat!
    Thanks, Joe.
    Jeff Higgins, May 14, 2006
    #4
    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. asd
    Replies:
    3
    Views:
    437
    Arnaud Berger
    May 23, 2005
  2. Michael Reiche
    Replies:
    3
    Views:
    10,844
    Michael Reiche
    Feb 5, 2004
  3. dogbite
    Replies:
    4
    Views:
    691
    osmium
    Oct 10, 2003
  4. Newbie

    Using XSLT to Order Nodes

    Newbie, Jul 20, 2006, in forum: XML
    Replies:
    5
    Views:
    662
    Newbie
    Jul 20, 2006
  5. jag
    Replies:
    0
    Views:
    586
Loading...

Share This Page