Re: [xsl] sorting by maximum value of multiple nodes

Subject: Re: [xsl] sorting by maximum value of multiple nodes
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 9 Feb 2006 11:28:14 +0530
If you are using XSLT 2.0, you can do something like this (tested with
saxon b 8.6.1)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       xmlns:xs="http://www.w3.org/2001/XMLSchema";
                       version="2.0">

<xsl:output method="text" />

<xsl:template match="/employees">
  <xsl:apply-templates select="employee">
    <xsl:sort select="max(for $x in Patent/date return xs:date($x))"
order="descending" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="employee">
  <xsl:value-of select="firstName" /><xsl:text>
</xsl:text><xsl:value-of select="lastName"
/><xsl:text>&#xa;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

On 2/8/06, Billie <whynot77@xxxxxxxxxxxx> wrote:
> Hi everyone,
> What I'd like to do is sort a list by the maximum value of a node that may
> appear multiple times.  I think I'll do much better explaining this in code
> rather than words, so here is the XML:
>
> <employees>
>        <employee>
>                <firstName>Joe</firstName>
>                <lastName>Black</lastName>
>                <Patent>
>                        <date>2005-10-13</date>
>                        <id>65-AHK</id>
>                </Patent>
>                <Patent>
>                        <date>2006-01-03</date>
>                        <id>65-AHK</id>
>                </Patent>
>                <Patent>
>                        <date>2004-08-24</date>
>                        <id>65-AHK</id>
>                </Patent>
>        </employee>
>        <employee>
>                <firstName>Jane</firstName>
>                <lastName>Doe</lastName>
>                <Patent>
>                        <date>2005-11-18</date>
>                        <id>65-AHK</id>
>                </Patent>
>                <Patent>
>                        <date>2006-01-19</date>
>                        <id>65-AHK</id>
>                </Patent>
>        </employee>
> <employees>
>
> I'm looking to sort this list by which employee has the most recent Patent,
so
> in this case, Jane Doe would be first because her most recent Patent has a
> date of 2006-01-19 and Joe Black's most recent Patent has a date of
2006-01-03.
>
> So I would want the XSL to be something like:
> <xsl:apply-templates select="employee">
>        <xsl:sort>
>                (what do I do here?)
>        </xsl:sort>
> </xsl:apply-templates>
>
> Thanks for any help!

Current Thread