Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2005, 03:31 PM
Registered User
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date format in XSLT

Hi,
I have a date in the format mm/dd/yy
and i need this transformed to yyyymmdd

Can somebody suggest the method in xslt?
Thanks
 
Old November 29th, 2005, 02:20 AM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes it is possbile. For that you have to write java extensions for xslt like
xmlns:java="XSLT.Extension.MyDateFormat"

here the java code


package XSLT.Extension;

import java.io.*;
import java.util.*;
import java.text.*;



public class MyDateFormat {


    public static String getDateFormat(String dt)
    {
        Date now = new Date();
        String outStr="";
        try
          {
                //===== converting String to date=========
                  String DATE_FORMAT = "dd/MM/yyyy";
                  SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);


                  Date d=null;

                  d=sdf.parse(dt);

                 //============ converting to yyyyMMdd format========
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                outStr=sdf1.format(d);


          }
          catch(Exception e)
          {
              System.out.println("error");
          }



        return outStr;
    }

    public static String getOuts()
    {

        return "TexTech international";
    }
}

compile the java code and put the directory in classpath and
add the xmlns:dt="XSLT.Extension.MyDateFormat" to the XSL file
transform using the Saxon8 processor
you will get the output




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dt="XSLT.Extension.MyDateFormat">

        <xsl:template match="/">
                    <p>

                            <xsl:value-of select="dt:getDateFormat('30/12/2004')"/>
                    </p>
        </xsl:template>
</xsl:stylesheet>




 
Old November 29th, 2005, 04:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't need extension functions to handle simple cases, just use something like

concat('20', substring($in, 7,2), '-', substring($in, 1,2), '-', substring($in,4,2))

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
date format differs, need to force format somehow patricolsson ASP.NET 2.0 Basics 1 December 3rd, 2009 12:53 AM
How to format date using XSLT navi25584 XSLT 3 September 2nd, 2008 07:00 AM
Convert British format date to American format? fyr PHP How-To 0 December 19th, 2007 03:17 PM
XSLT - Convert the date in different format haixia XSLT 1 August 25th, 2006 11:51 AM
date format differs, need to force format somehow patricolsson HTML Code Clinic 2 January 12th, 2006 05:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.