XSLT: Select nodes in reverse order

T

thrill5

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
 
J

Jeff Higgins

thrill5 said:
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>
 
J

Joe Kesselman

Jeff said:
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>
 
J

Jeff Higgins

Joe said:
... 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top