Forum Moderators: open

Message Too Old, No Replies

Sorting in XSLT & output all nodes

         

majjk

12:29 am on Feb 19, 2007 (gmt 0)

10+ Year Member



I would like to perform something that seems very basic, but I just can't seem to find out how to do it. I have the following document:

<employees>

<employee hireDate="04/23/1999">
<last>Hill</last>
<first>Phil</first>
<salary>100000</salary>
</employee>

<employee hireDate="09/01/1998">
<last>Herbert</last>
<first>Johnny</first>
<salary>95000</salary>
</employee>

<employee hireDate="08/20/2000">
<last>Hill</last>
<first>Graham</first>
<salary>89000</salary>
</employee>

</employees>

and I would like to sort on "salary". I would also like to output all nodes. In this simple example I could of course specify every node I want to output, but I am in a position where I'm not sure which nodes I might have in the future. The only thing I know for sure is that I want to sort on "salary". The following would sort on "salary":

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

<xsl:output method="text"/>

<xsl:template match="employees">
<xsl:apply-templates>
<xsl:sort select="salary"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="employee">
Last: <xsl:apply-templates select="last"/>
First: <xsl:apply-templates select="first"/>
Salary: <xsl:apply-templates select="salary"/>
Hire Date: <xsl:apply-templates select="@hireDate"/>
<xsl:text>
</xsl:text>

</xsl:template>

</xsl:stylesheet>

but how can I change this so that it will continue to output all info, even if I in the future add more nodes?

To put it in a different way, I just want to transform the document (sort on something) while leaving all info intact. Ideas?