xsl-list
[Top] [All Lists]

RE: Date formatting using XSLT extensions functions

2003-04-28 01:43:56
I'm not sure if this helps but I define the months as variables:-
<!-- MONTHS -->
        <xsl:variable name="jan" select="'January'"/>
        <xsl:variable name="feb" select="'February'"/>
        <xsl:variable name="mar" select="'March'"/>
        <xsl:variable name="apr" select="'April'"/>
        <xsl:variable name="may" select="'May'"/>
        <xsl:variable name="jun" select="'June'"/>
        <xsl:variable name="jul" select="'July'"/>
        <xsl:variable name="aug" select="'August'"/>
        <xsl:variable name="sep" select="'September'"/>
        <xsl:variable name="oct" select="'October'"/>
        <xsl:variable name="nov" select="'November'"/>
        <xsl:variable name="dec" select="'December'"/>

and then I use a template to match the date as follows:-

<xsl:template name="date-to-text">
                <xsl:param name="InDate"/>
                <xsl:choose>
                        <xsl:when test="substring($InDate, 4, 2)='01'">
                                <xsl:value-of select="$jan"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='02'">
                                <xsl:value-of select="$feb"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='03'">
                                <xsl:value-of select="$mar"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='04'">
                                <xsl:value-of select="$apr"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='05'">
                                <xsl:value-of select="$may"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='06'">
                                <xsl:value-of select="$jun"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='07'">
                                <xsl:value-of select="$jul"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='08'">
                                <xsl:value-of select="$aug"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='09'">
                                <xsl:value-of select="$sep"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='10'">
                                <xsl:value-of select="$oct"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='11'">
                                <xsl:value-of select="$nov"/>
                        </xsl:when>
                        <xsl:when test="substring($InDate, 4, 2)='12'">
                                <xsl:value-of select="$dec"/>
                        </xsl:when>
                </xsl:choose>
                &#160;
                <xsl:value-of select="substring($InDate, 1, 2)"/>,&#160;
                <xsl:value-of select="substring($InDate, 7, 4)"/> at GMT
                <xsl:value-of 
select="//DataResponse/IccReport/@ReportGenerationTime"/>
        </xsl:template>

-----Original Message-----
From: Kaine Varley [mailto:kaine(_dot_)varley(_at_)getrealsystems(_dot_)com]
Sent: 28 April 2003 08:26
To: 'XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: [xsl] Date formatting using XSLT extensions functions


Hi,

I have an input date in a format like this: 13/03/2003 4:58:32 pm which I'd
like to format into the following: 13 March

I have tried the following two extension functions, without success I'm
afraid. The first uses VBScript and the second JavaScript. I haven't
attempted to convert the month number to a name yet. Are the built script
language functions and objects not available from the extensions?

VBScript version:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:extra="urn:extra-functions">
        <msxml:script xmlns:msxml="urn:schemas-microsoft-com:xslt"
language="VBScript" implements-prefix="extra">
                Function DateFormat(InputDate)
                        If IsDate(InputDate) Then
                                DateFormat = Day(InputDate) & " " &
Month(InputDate)
                        End If
                End Function
        </msxml:script>

        <xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>



        <xsl:template name="format-date">
                <xsl:param name="date-to-format" />


                <xsl:value-of select="extra:DateFormat($date-to-format)" />
        </xsl:template>

</xsl:stylesheet>

 

JavaScript version:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:extra="urn:extra-functions">

        <msxml:script xmlns:msxml="urn:schemas-microsoft-com:xslt"
language="JavaScript" implements-prefix="extra">
                function formatDate(dte){
                        var dateToFormat = new Date(dte);

                        return dateToFormat.getDay() + ' ' +
dateToFormat.getMonth();
                };
        </msxml:script>

        <xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>



        <xsl:template name="format-date">
                <xsl:param name="date-to-format" />


                <xsl:value-of select="extra:formatDate($date-to-format)" />
        </xsl:template>

</xsl:stylesheet>
 

Regards,


Kaine

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


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