Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - XSLT: Select nodes in reverse order

 
Thread Tools Search this Thread
Old 05-14-2006, 01:59 AM   #1
Default XSLT: Select nodes in reverse order


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
  Reply With Quote
Old 05-14-2006, 03:54 AM   #2
Jeff Higgins
 
Posts: n/a
Default 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
  Reply With Quote
Old 05-14-2006, 04:54 AM   #3
Joe Kesselman
 
Posts: n/a
Default 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
  Reply With Quote
Old 05-14-2006, 02:10 PM   #4
Jeff Higgins
 
Posts: n/a
Default 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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kodak Easyshare - manual order probs John Bailey General Help Related Topics 0 01-08-2009 04:37 PM
Why are TV shows on DVD Out Of Order? mhadley DVD Video 3 09-20-2007 11:56 PM
DVD Verdict reviews: LAW AND ORDER: SPECIAL VICTIMS UNIT: THE FIFTH YEAR and more! DVD Verdict DVD Video 0 11-03-2004 10:12 AM
Viewing order for Batman: The Animated Series? Robert Kaiser DVD Video 11 07-25-2004 03:15 AM
New Releases: Apprentice, Magnum, Munsters & Law & Order: Updated complete downloadable R1 DVD DB & info lists Doug MacLean DVD Video 1 06-05-2004 03:04 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46