RE: Sort

Subject: RE: Sort
From: Nate Austin <naustin@xxxxxxxxxxxxxxx>
Date: Mon, 31 Jul 2000 16:26:56 -0500
Jon -

The only XSLT solution that I came up with is very ugly, so I'll start with
the java one:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParsePosition;

public class DateConv
{

	public DateConv()
	{}

	public String getDate(String dateIn, String formatIn, String
formatOut)
	{
		try
		{
			if( dateIn.equals("now") )
			{
				return (new
SimpleDateFormat(formatOut).format(new Date()));
			}
			else
			{
				return (new
SimpleDateFormat(formatOut)).format(
		               (new SimpleDateFormat(formatIn)).parse(
		                 dateIn, new ParsePosition(0)));
		    }
		}
		catch( Exception e )
		{
			return "";
		}
	}
}

This was posted to the list awhile back (I don't remember who posted it, but
I liked it).  Now here's the stylesheet using xalan's extension:

<?xml version="1.0"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                             xmlns:java="http://xml.apache.org/xslt/java";
                             exclude-result-prefixes="java">
                             
  <xsl:output method="xml"/>
  
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:variable name="dateconv" select="java:DateConv.new()"/>
  
  <xsl:template match="marquees">
    <xsl:apply-templates select="Marquee">
      <xsl:sort
select="java:getDate($dateconv,string(@Modification_date),'EEEE, MMMM dd,
yyyy hh:mm:ss a z','yyyyMMddHHmmss')"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="Marquee">
    <xsl:value-of select="@Class"/>
  </xsl:template>
  
</xsl:transform>

That's the easy way.  The hard way would basically be a template to parse
the date string manually using the xslt string handling functions and return
something similar to this. (Ordered such that a later date/time will be a
larger number).  Tested using Xalan 1.0.1.

I'm sure there're other solutions that the pros out there'll find, but my
inexperienced mind doesn't stretch that far quite yet.

-Nate


> Date: Mon, 31 Jul 2000 07:22:27 -0700
> From: "Jon Wynacht" <jwynacht@xxxxxxxxx>
> Subject: Sort
>
> Hi,
>
> I have a sorting question for the group. I have elements like the
following:
>
> <Marquee Class="RIO_container" Expiration_day="Indefinite"
> Expiration_month="Indefinite" Expiration_year="Indefinite"
> Id="vt_182-Virtual_teams-Marquee.xml" Last_modifier="admin"
> Maximum_number="" Modification_date="Thursday, July 20, 2000 10:38:21 AM
> PDT" Owner="admin" Progress="complete" Status="modify" Used_in=" vt">
>
> I want to sort by the Modification_date attribute. How can I do this
> efficiently with XSLT? Or maybe I just can't? I've tried using a
JavaScript
> extension to manipulate the numbers but it just didn't seem to work. I'd
> prefer not to do it with Java but with if I have to.
>
> Please advise.
>
> Thanks,
>
> Jon


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • Sort
    • Jon Wynacht - Mon, 31 Jul 2000 07:22:27 -0700
      • <Possible follow-ups>
      • Spychalski, Frank - Mon, 31 Jul 2000 18:14:06 +0200
      • Oliver Becker - Mon, 31 Jul 2000 18:19:37 +0200 (MET DST)
      • Nate Austin - Mon, 31 Jul 2000 16:26:56 -0500 <=