Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

xsl date format issue

255 views
Skip to first unread message

kamkaro

unread,
Jul 6, 2007, 3:44:00 PM7/6/07
to
Hi , i am having problem to convert date (20010306) to (March 06,2001).
Actually, In my xml file i have <date> 20010306</date> and would like to
display March 06,2001. Is anybody help me out plz. i really appreciate that.
Thanks

Martin Honnen

unread,
Jul 7, 2007, 10:41:32 AM7/7/07
to

If you have an XSD date in the form of 2001-03-06 then with MSXML (4 and
later) and with XslCompiledTransform you can use e.g.

<xsl:value-of
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
select="msxsl:format-date('2001-03-06', 'MMMM dd, yyyy', 'en-US')"/>

So consider storing your dates in the yyyy-mm-dd format to be able to
use the format-date function. If you use yyyymmdd then you first need to
convert it to yyyy-mm-dd.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

kamkaro

unread,
Jul 7, 2007, 9:18:01 PM7/7/07
to
Thanks Martin. I have to say, you have very strong knowledge about XML. i
have two more questions, first i m using Java base parser so can i use
Microsoft stuff, second according to your reply you give me the following
code;

<xsl:value-of
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
select="msxsl:format-date('2001-03-06', 'MMMM dd, yyyy', 'en-US')"/>

how do i call the date element's data, e.g i m doing
<xsl:template match="Date">
<xsl:value-of select="." />
</xsl:template>

would you plz explain me with example, i really appreciate your help. thanks

Message has been deleted

Brandon Gano

unread,
Jul 16, 2007, 12:34:54 PM7/16/07
to
Here is a solution that should work with any XSL processor (not tested):

<xsl:template name="expand-date">

<!-- (YYYYMMDD) -->
<xsl:param name="date" />

<!-- Parse date -->
<xsl:variable name="year" select="substring($date,1,4)" />
<xsl:variable name="month" select="number(substring($date,5,2))" />
<xsl:variable name="day" select="number(substring($date,7,2))" />

<!-- Output -->
<xsl:choose>
<xsl:when test="$month=1">January</xsl:when>
<xsl:when test="$month=2">February</xsl:when>
<xsl:when test="$month=3">March</xsl:when>
<xsl:when test="$month=4">April</xsl:when>
<xsl:when test="$month=5">May</xsl:when>
<xsl:when test="$month=6">June</xsl:when>
<xsl:when test="$month=7">July</xsl:when>
<xsl:when test="$month=8">August</xsl:when>
<xsl:when test="$month=9">September</xsl:when>
<xsl:when test="$month=10">October</xsl:when>
<xsl:when test="$month=11">November</xsl:when>
<xsl:when test="$month=12">December</xsl:when>
</xsl:choose>
<xsl:value-of select="$day" /><xsl:text>, </xsl:text>
<xsl:value-of select="$year" />

</xsl:template>

There is probably a cleaner/more efficient solution for parsing the month
name, but this should work. Here is how you might call the template:

<xsl:call-template name="expand-date">
<xsl:with-param name="date" select="/path/to/@date-node" />
</xsl:call-template>

-bgano

----- Original Message -----
From: "Martin Honnen" <maho...@yahoo.de>
Newsgroups: microsoft.public.xsl
Sent: Sunday, July 08, 2007 4:30 AM
Subject: Re: xsl date format issue


> kamkaro wrote:
>> Thanks Martin. I have to say, you have very strong knowledge about XML. i
>> have two more questions, first i m using Java base parser so can i use
>> Microsoft stuff, second according to your reply you give me the following
>> code;
>> <xsl:value-of
>> xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>> select="msxsl:format-date('2001-03-06', 'MMMM dd, yyyy', 'en-US')"/>
>>
>> how do i call the date element's data, e.g i m doing <xsl:template
>> match="Date">
>> <xsl:value-of select="." />
>> </xsl:template>
>

> An XSLT processor implemented in Java does very likely not implement those
> MS specific extension functions my suggested solution uses. It might offer
> other extension functions, in particular the possibility to use Java
> objects and methods but I am not familiar with that. Try a
> forum/newsgroup/mailing list dedicated to that XSLT processor you use.
>
> As for the template, you would simply use


>
> <xsl:template match="Date">
> <xsl:value-of

> xmlns:msxsl="urn:schemas-microsoft-com:xslt"

> select="msxsl:format-date(., 'MMMM dd, yyyy', 'en-US')"/>
> </xsl:template>
>
> assuming Date has contents in the form yyyy-mm-dd and you are using MSXML
> 4 or later or XslCompiledTransform.

ahill.e...@gmail.com

unread,
Jul 16, 2012, 3:29:05 PM7/16/12
to
THANK YOU, BRANDON!

I have never done XSL (have tried to stay away from it), but found myself needing to write a handful. This saved my ass!

0 new messages