This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Sort


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@cisco.com>
> 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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]