472,989 Members | 3,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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
May 14 '06 #1
3 3957

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>
May 14 '06 #2
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
May 14 '06 #3

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.
May 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
5
by: Marcel Akkerman | last post by:
Hi, Does anyone have a clue how to reduce the number of nodes using XSLT? When outputing all nodes in order I could just use <xsl:for-each select="name"> But what if I, besides sorting and...
5
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case...
2
by: Jon Martin Solaas | last post by:
Hi, I have a general document somewhat like this: -------------------------------------- <root> <level1> <level2> <interestingstuff number="2"/> <interestingstuff number="3"/>...
3
by: Alex | last post by:
I stumbled upon this while developing a custom XPathNavigator. It appears that copy action for attributes is broken in the .net framework XSLT processor. The intent was to just copy the entities...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
8
by: Vikram Vamshi | last post by:
If I execute XmlNode.SelectNodes("some xpath query") and then use foreach to iterate over it. Will it guaratee that the order of nodes I get will be same as the order of nodes in the original xml...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.