Discussion:
Date formatting using XSLT extensions functions
Kaine Varley
2003-04-28 07:25:44 UTC
Permalink
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
J***@nokia.com
2003-04-28 08:18:57 UTC
Permalink
Hi,
Post by Kaine Varley
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
Pure XSLT would be something like

<xsl:template match="/">
<xsl:variable name="x" select="'13/03/2003 4:58:32 pm'" />
<xsl:value-of select="substring-before($x, '/')" />
<xsl:text> </xsl:text>
<xsl:value-of select="document('')/*/x:months/month[@number = format-number(substring-before(substring-after($x, '/'), '/'), '##')]/@name" />
</xsl:template>

<x:months>
<month name="January" number="1" />
<month name="February" number="2" />
<month name="March" number="3" />
<month name="April" number="4" />
<month name="May" number="5" />
<month name="June" number="6" />
<month name="July" number="7" />
<month name="August" number="8" />
<month name="September" number="9" />
<month name="October" number="10" />
<month name="November" number="11" />
<month name="December" number="12" />
</x:months>

but you can also use <http://exslt.org/date/index.html> if your processors support EXSLT date and time module.


Cheers,

Jarno - Lights Of Euphoria: True Life (VNV Nation)

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Cross, Neal - Development Controller
2003-04-28 08:43:56 UTC
Permalink
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:***@getrealsystems.com]
Sent: 28 April 2003 08:26
To: 'XSL-***@lists.mulberrytech.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
Kaine Varley
2003-04-28 12:45:40 UTC
Permalink
Neal,

Thanks for the suggestion; I'm sure I'll use some aspects of it.
However, rather than parsing the date as a string value, i.e. the
characters at position 4 and 5 equate to the month, I was hoping to deal
with the date as a proper date. The reason for this is because different
databases have ever so slightly different ideas of what a date looks
like, but the date handling objects in the scripting languages appear to
deal with this. For instance a date could appear in any of the following
formats:

- 13/03/2003 4:58:32 pm
- 2003-03-13 16:58:35
- March 13, 2003


As you see simply parsing the date would give unexpected results, even
though the dates validate OK using the VBScript IsDate() function.
Besides, I suppose I'd like to know what I'm doing wrong when calling
the extension function.

Thanks again for your feedback.


Kaine



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Kaine Varley
2003-04-28 12:48:28 UTC
Permalink
Jarno,

Thanks for the excellent feedback, definitely food for thought. I was
hoping to avoid string parsing since I cannot guarantee the date format.
However, having had a quick look at the site you recommended,
http://exslt.org/date/index.html, I feel certain it will help me.

Thanks again,


Kaine



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
D***@rnib.org.uk
2003-04-28 13:21:06 UTC
Permalink
How about identifying the source of the date?
That way you could fix its format and deal with it.

No software I know of would take (almost) a random
string and figure it out as a date?

Otherwise, use whatever language your processor has
access to and parse the string char by char in
an external function.

xslt 2 has date functions, but they won't help you
if you don't know what format they are in.

regards DaveP
Post by Kaine Varley
Thanks for the suggestion; I'm sure I'll use some aspects of it.
However, rather than parsing the date as a string value, i.e. the
characters at position 4 and 5 equate to the month, I was
hoping to deal
with the date as a proper date. The reason for this is
because different
databases have ever so slightly different ideas of what a date looks
like, but the date handling objects in the scripting
languages appear to
deal with this. For instance a date could appear in any of
the following
- 13/03/2003 4:58:32 pm
- 2003-03-13 16:58:35
- March 13, 2003
As you see simply parsing the date would give unexpected results, even
though the dates validate OK using the VBScript IsDate() function.
Besides, I suppose I'd like to know what I'm doing wrong when calling
the extension function.
Thanks again for your feedback.
Kaine
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
-

NOTICE: The information contained in this email and any attachments is
confidential and may be legally privileged. If you are not the
intended recipient you are hereby notified that you must not use,
disclose, distribute, copy, print or rely on this email's content. If
you are not the intended recipient, please notify the sender
immediately and then delete the email and any attachments from your
system.

RNIB has made strenuous efforts to ensure that emails and any
attachments generated by its staff are free from viruses. However, it
cannot accept any responsibility for any viruses which are
transmitted. We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email
and any attachments are those of the author and do not necessarily
represent those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Kaine Varley
2003-04-28 14:04:10 UTC
Permalink
DaveP,

Thanks for the advice. It is looking more and more likely that I'll have
to become stricter with the initial date format being input. However, as
I mentioned, the date formats I described are all parsed and handled OK
through the VBScript Date functions, when used in ASP page or VB, so I
was hoping to access those functions through the use of an XSLT
extension function, as described below and in the original thread, since
I can, at least, guarantee the platform that it will be running on.
However, I have failed in my attempt to use the VBScript Date functions
as extensions. Any ideas?

Thanks again for the valuable input.


Kaine


(Code from original thread)
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>


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Eugene Kuznetsov
2003-04-28 18:38:19 UTC
Permalink
Kaine,

Just to reiterate something important: there is a lot to be said
for avoiding VBScript or java extension functions if at all possible.
After all, part of the reason for using XML, XPath and XSLT is to have a
cross-platform, portable, easy-to-integrate way of doing integration. If
your XML app still requires several different language runtimes,
including custom VBSCript, some might say that you could've used VB for
the whole app. You would be missing out on the ability to seamlessly
move your XML processing between different platforms, servers and into
network hardware.
For that reason, using often-inconvenient string-parsing
primitives or the increasingly-accepted "exslt" extensions is better
than writing your own extension functions. Date formatting is one of the
top complaints against XSLT 1.0, so there's a broad set of common
approaches for dealing with it: XSLT 2.0, exslt, common templates, etc.
Even if it seems a little more painful, a little investment in
portability today will pay off in a big way later.
Regards,


\\ Eugene Kuznetsov
\\ ***@datapower.com - XML-aware network infrastructure
\\ DataPower Technology, Inc. - secure SOAP, speed up XPath
\\ http://www.datapower.com - XS40 XML Security Gateway



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Dimitre Novatchev
2003-04-29 04:57:26 UTC
Permalink
http://www.topxml.com/code/default.asp?p=3&id=v20021212030100


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Post by Kaine Varley
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?
<?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>
<?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
Loading...