Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2008, 08:15 AM
Registered User
 
Join Date: Jun 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reversing nodeset

Hi,

I have a nodeset like given below
<d1>12</d1>
<d1>dfd</d1>
<d1>tyy</d1>
<d1>99</d1>
<d1>klgh</d1>

i would like to reverse it like
<d1>klgh</d1>
<d1>99</d1>
<d1>tyy</d1>
<d1>dfd</d1>
<d1>12</d1>

the number of elements in nodeset can be dynamic.

How can i do it using xslt?

Any help in this regards will be greatly appreciated..

Thanks in advance,
sudhish


 
Old June 19th, 2008, 08:24 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can process the elements in reverse order using <xsl:sort select="position()"/>:

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="d1">
        <xsl:sort select="position()" data-type="number" order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="d1">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old June 19th, 2008, 11:07 AM
Registered User
 
Join Date: Jun 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank You Martin. That is great. I never thought we can use xsl:sort for this purpose. Thank You Once Again.

 
Old June 19th, 2008, 11:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Or in 2.0, use the reverse() function:

<xsl:apply-templates select="reverse(d1)">...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
select nodeset newbies1234 XSLT 4 August 22nd, 2008 10:52 AM
with-param nodeset RoeZ XSLT 6 November 1st, 2007 06:53 PM
comparing nodeset stolte XSLT 7 May 30th, 2007 03:58 PM
Optional Reversing An Array (pgs. 133 to 134) TekWarrior BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 February 24th, 2007 02:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.