RE: How is date:date-format used?

Hack Kampbjørn <[email protected]>
Newsgroups gmane.text.xml.xslt.extensions
Message-ID <[email protected]>
With this patch against Xalan 2.7.0 (very rough I still need to understand their localization and test code) the following XSLT file

<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:date="http://exslt.org/dates-and-times"
	extension-element-prefixes="date">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <date:date-format lang="en"/>
    <date:date-format name="danish" lang="da"/>
    <xsl:value-of select="date:format-date('2005-11-08', 'EEE, dd MMM', 'danish')\/>
    <xsl:value-of select="date:format-date('2005-11-08', 'EEE, dd MMM')"/>
  </xsl:template>
</xsl:stylesheet>

produces first "ti, 08 nov" and then "Tue, 08 Nov"

I would apreciate it if somebody could take a look it or just comment the testcase before submitting it to the Xalan people.


> -----Original Message-----
> From: Hack Kampbjørn 
> Sent: 8. november 2005 12:04
> To: [email protected]
> Subject: RE: [exslt] How is date:date-format used?
> 
> 
> Is there any example of how date:date-format is intended to 
> be used? I would like to add support for it to Xalan but 
> unfortunately the implementers page did not provide an 
> example of using a named format.
> 
> Google is not much help as date-format matches 
> date:format-date and "date:date-format" matches "date date format"
> 
> 
> > -----Original Message-----
> > From: Hack Kampbjørn 
> > Sent: 1. november 2005 18:13
> > To: [email protected]
> > Subject: [exslt] How is date:date-format used?
> > 
> > 
> > We have an XML date that should be formated in either danish 
> > or english. Right now we the default locale is danish so 
> > date:format-date() produces danish day and month names.
> > 
> > Looking at the web-site there is the element date:date-format 
> > to address this issue. After reading the documentation 
> > several times I cannot figure out how to use it.
> > http://www.exslt.org/date/elements/date-format/index.html
> > 
> > Google gave me this old mail that references decimal-format 
> > and format-number which takes an optional third argument that 
> > is the decimal-format name. If this is the idea and the 
> > format-date documentation has not been updated with the third 
> > date-format argument?
> > 
> > <date:date-format name="danish" lang="da"/>
> > <date:date-format name="english" lang="en"/>
> > <xsl:value-of select="date:format-date(departure_time, 'EEE 
> > dd MMM YYYY', danish)"/>
> > <xsl:value-of select="date:format-date(departure_time, 'EEE 
> > dd MMM YYYY', english)"/>
> > 
> > http://lists.fourthought.com/pipermail/exslt/2001-April/000022.html
> > 
> > 
> > Hack Kampbjørn
> > 2M business application a|s
> > +45 3956 3955
> > +45 3953 0780
> > _______________________________________________
> > exslt mailing list
> > [email protected]
> > http://www.exslt.org/list
> > 
> _______________________________________________
> exslt mailing list
> [email protected]
> http://www.exslt.org/list
>

_______________________________________________
exslt mailing list
[email protected]
http://www.exslt.org/list
exslt-date-format.diff.txt (text/plain, 5.5 KB)
--- src/org/apache/xalan/extensions/MethodResolver.java.orig	2005-08-06 23:05:30.000000000 +0200
+++ src/org/apache/xalan/extensions/MethodResolver.java	2005-11-08 17:40:54.609026000 +0100
@@ -322,6 +322,8 @@
   {
     // System.out.println("---> Looking for element method: "+name);
     // System.out.println("---> classObj: "+classObj);
+    if (name.indexOf("-")>0)
+      name = replaceDash(name);
     Method bestMethod = null;
     Method[] methods = classObj.getMethods();
     int nMethods = methods.length;
--- src/org/apache/xalan/lib/ExsltDatetime.java.orig	2005-08-06 23:05:30.000000000 +0200
+++ src/org/apache/xalan/lib/ExsltDatetime.java	2005-11-08 18:01:16.301369200 +0100
@@ -24,9 +24,14 @@
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Locale;
 import java.util.TimeZone;
+import javax.xml.transform.TransformerException;
 
+import org.apache.xalan.extensions.XSLProcessorContext;
+import org.apache.xalan.templates.ElemExtensionCall;
 import org.apache.xpath.objects.XBoolean;
 import org.apache.xpath.objects.XNumber;
 import org.apache.xpath.objects.XObject;
@@ -58,6 +63,36 @@
     static final String t = "HH:mm:ss";
     static final String EMPTY_STR = "";
 
+    static Locale defaultFormat = null;
+    static final Map namedFormats = new HashMap();
+
+    //static {
+	//namedFormats.put("da", new Locale("da"));
+	//namedFormats.put("en", Locale.ENGLISH);
+    //}
+	    
+    public static void dateFormat(XSLProcessorContext context, ElemExtensionCall extension)
+	    throws TransformerException
+    {
+	String name = extension.getAttribute("name", context.getContextNode(), context.getTransformer());
+	String lang = extension.getAttribute("lang", context.getContextNode(), context.getTransformer()); 
+	if (lang != null) {
+	    if (name == null) {
+		if (defaultFormat == null) {
+		    defaultFormat = new Locale(lang);
+		} else if (!lang.equals(defaultFormat.toString())) {
+		    throw new TransformerException("default date format redefined from '" + defaultFormat.toString() + "' to '" + lang + "'");
+		}
+
+	    } else {
+		Object current = namedFormats.put(name, new Locale(lang));
+		if (current != null && !lang.equals(current.toString())) {
+		    throw new TransformerException("date format for '" + name + "' redefined from '" + current.toString() + "' to '" + lang + "'");
+		}
+	    }
+	}
+    }
+
     /**
      * The date:date-time function returns the current date and time as a date/time string. 
      * The date/time string that's returned must be a string in the format defined as the 
@@ -953,9 +988,17 @@
      */
     public static String formatDate(String dateTime, String pattern)
     {
+	return formatDate(dateTime, pattern, null);
+    }
+    public static String formatDate(String dateTime, String pattern, String formatName)
+    {
         final String yearSymbols = "Gy";
         final String monthSymbols = "M";
         final String daySymbols = "dDEFwW";
+	Locale lang = (Locale) namedFormats.get(formatName);
+	if (lang == null) {
+	    lang = defaultFormat == null ? Locale.getDefault() : defaultFormat;
+	}
         TimeZone timeZone;
         String zone;
 
@@ -1006,7 +1049,7 @@
             inFormat.setLenient(false);
             Date d= inFormat.parse(dateTime);
             SimpleDateFormat outFormat = new SimpleDateFormat(strip
-                (yearSymbols + monthSymbols + daySymbols, pattern));
+                (yearSymbols + monthSymbols + daySymbols, pattern), lang);
             outFormat.setTimeZone(timeZone);
             return outFormat.format(d);
         }
@@ -1022,7 +1065,7 @@
                 SimpleDateFormat inFormat = new SimpleDateFormat(formats[i]);
                 inFormat.setLenient(false);
                 Date d = inFormat.parse(dateTime);
-                SimpleDateFormat outFormat = new SimpleDateFormat(pattern);
+                SimpleDateFormat outFormat = new SimpleDateFormat(pattern, lang);
                 outFormat.setTimeZone(timeZone);
                 return outFormat.format(d);
             }
@@ -1040,7 +1083,7 @@
             SimpleDateFormat inFormat = new SimpleDateFormat(gmd);
             inFormat.setLenient(false);          
             Date d = inFormat.parse(dateTime);
-            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));
+            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern), lang);
             outFormat.setTimeZone(timeZone);
             return outFormat.format(d);
         }
@@ -1052,7 +1095,7 @@
             SimpleDateFormat inFormat = new SimpleDateFormat(gm);
             inFormat.setLenient(false);
             Date d = inFormat.parse(dateTime);
-            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));
+            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern), lang);
             outFormat.setTimeZone(timeZone);
             return outFormat.format(d);
         }
@@ -1064,7 +1107,7 @@
             SimpleDateFormat inFormat = new SimpleDateFormat(gd);
             inFormat.setLenient(false);
             Date d = inFormat.parse(dateTime);
-            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols + monthSymbols, pattern));
+            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols + monthSymbols, pattern), lang);
             outFormat.setTimeZone(timeZone);
             return outFormat.format(d);
         }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.