Avatar of sharepointuser
sharepointuser
 asked on

Format date in xslt

Hi,

I am getting date as 2/28/2009.
How can I format it as Feb 28 2009.

Thanks
Web Languages and Standards

Avatar of undefined
Last Comment
abel

8/22/2022 - Mon
abel

I hope you can use either EXSLT (for XSLT 1.0) or XSLT 2.0. In the latter case you can use the format-date functions.
abel

Note that XSLT 1.0 does not have intrinsic date formatting functions. If you can get a copy of the O'Reilly's XSLT Cookbook, 2nd edition, then they provide you with code samples to do it in pure XSLT 1.0 (quite lengthy, but includes short written date format).
ASKER CERTIFIED SOLUTION
abel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sharepointuser

ASKER
Which version does sharepoint MOSS 2007 use?
I am trying to do this in the search core results web part.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sharepointuser

ASKER
Ok. I found that it uses version 1.0
abel

Sharepoint is from Microsoft and Microsoft natively only supports version 1.0, though there are upgrading plans, that will take a while still.
sharepointuser

ASKER
the first line states as <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
 so it is v1.0 right?
I am new to this. Please let me know how I can integrate the various files in my xslt file.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
abel

An example of how to call it is below. Make sure you download the string functions too and that you change the import path to your path in the format xslt as below:

<xsl:import href="str-functions.xslt" />

If you have additional questions, I'll be back tomorrow (have to go now..)

<xsl:stylesheet version="1.0"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:import href="format-date.xslt"/>
 
    <xsl:template match="/">
        <xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="'1997-07-16'" />
            <xsl:with-param name="pattern" select="'MMM DD yyyy'" />
        </xsl:call-template>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

sharepointuser

ASKER
I have added the template inline as in the attached code snippet.
But I am getting an error while rendering the web part
I have made the following modifications.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
                xmlns:str="http://exslt.org/strings"
                xmlns:func="http://exslt.org/functions"
                extension-element-prefixes="date str func"
> - Modified the tag with the extra parameters

<xsl:import href="str.padding.function.xsl" />
- I did not find the code for this xsl
 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:date="http://exslt.org/dates-and-times"
                xmlns:str="http://exslt.org/strings"
                xmlns:func="http://exslt.org/functions"
                extension-element-prefixes="date str func"
> - Modified the tag with the extra parameters
 
<xsl:import href="str.padding.function.xsl" />
- I did not find the code for this xsl
 
<date:months>
...
</date:months>
<date:days>
...
</date:days>
<func:function name="date:format-date">
...
</func:function>
<func:function name="date:_format-date">
...
</func:function>
<func:function name="date:_week-in-year">
...
</func:function>

Open in new window

sharepointuser

ASKER
ok I resolved that error after I removed the line <xsl:import href="str.padding.function.xsl" />
Now I am getting the error while calling the function
Can we call the function as a template.
date:format-date is declared as a function however we are calling it as a template.
and I am also suspecting something in the format of the input and output dates
My input format is 2/28/2009 and I want the out put format as
Feb 28 2009
<xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="myVariable" />
            <xsl:with-param name="pattern" select="'MMM DD yyyy'" />
   </xsl:call-template>

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
sharepointuser

ASKER
I modified it to use the xslt instead of exslt as follows.
Now it is throwing error even when I am not calling the template.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:date="http://exslt.org/dates-and-times"
                xmlns:str="http://exslt.org/strings"
                extension-element-prefixes="date str">
<date:months>
...
</date:months>
<date:days>
...
</date:days>
<xsl:template name="date:format-date">
...
</xsl:template>
<xsl:template name="date:_format-date">
...
</xsl:template>
<xsl:template name="date:_week-in-year">
...
</xsl:template>

Open in new window

sharepointuser

ASKER
I found the problem statement.
It is <xsl:call-template name="str:padding">
str:padding template is not defined anywhere
sharepointuser

ASKER
I have resolved the errors but the calltemplate is returning empty.
I am calling the template as
<xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="myVariable" />
            <xsl:with-param name="pattern" select="'MMM DD yyyy'" />
</xsl:call-template>
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sharepointuser

ASKER
Is the format of my input and the required format correct?
Input 2/28/2009
Required: MMM DD yyyy
abel

> Is the format of my input and the required format correct?

No it is not correct, the format required by the template is a ISO 8601 date (common in all w3c standards: http://www.w3.org/TR/NOTE-datetime). Yet, there's another template from Jeni Tenison to help you with transforming a given date format into an ISO 8601 date.

> str:padding template is not defined anywhere

Remember my note on downloading the string functions xslt, quote: " Make sure you download the string functions too and that you change the import path to your path in the format xslt as below:". The direct link is: http://www.exslt.org/str/functions/padding/str.padding.template.xsl. Sorry for the unclarity about this.
abel

Here you go. I was a bit mistaken by the picture string, as the EXSLT takes a picture string equal to the format described for the class SimpleDateFormat under the section "Time format syntax". It is the little differences: "DD" would mean "the day in the year", "dd" would mean "the day in the month". Hence my mistake.

Anyway, here you go. As input, I took your format. The output is "Feb 28 2009". Download the format-date template and rename it parse-date.xslt: http://www.exslt.org/date/functions/format-date/date.format-date.template.xsl. There's an error in that template on line 1196:

</xsl:when>>

must be changed in:

</xsl:when>

Now, try the template as in the code snippet. It should output the following:

2/28/2009
2009-02-28
Feb 28 2009

If you have the test template working, it shouldn't be too hard to get it into your existing code.

<xsl:stylesheet version="1.0"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:import href="parse-date.xslt" />
    <xsl:import href="format-date.xslt"/>
 
    <xsl:output method="text" />
 
    <xsl:param name="date" select="'2/28/2009'" />
 
    <xsl:variable name="formattedDate">
        <xsl:call-template name="date:parse-date">
            <xsl:with-param name="date-time" select="$date" />
            <xsl:with-param name="format" select="'M/d/y'" />
        </xsl:call-template>
    </xsl:variable>
 
    <xsl:template match="/">
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="$date"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="$formattedDate"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="$formattedDate" />
            <xsl:with-param name="pattern" select="'MMM dd yyyy'" />
        </xsl:call-template>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
abel

(note that the solution to your original problem is in the previous post, this is a follow-up example meant as a tutorial)

A bit of explanation on how to use it yourself. Suppose you have a date: "16-09-1976" (slightly different format than yours) and you want an output like "16 September 1976 (is the 260th day and the 38th week of 1976)" you gotta go through a few steps:
  1. Change the picture string for the input date to feed for parse-date: "dd-MM-yyyy"
  2. Change the picture string for the output date "16 September 1976": "dd MMMM yyyy"
  3. Create a picture string for the day in the year: "D"
  4. Create a picture string for the week in the year: "w"
  5. Put the three elements in their own variables for easier concatenation and readability
  6. Arrange the output interspersed with the English text
The result of the example below (I changes some names for readability) is as follows:

input date: 16-09-1976
iso date:   1976-09-16
full date:  16 September 1976 (is the 260th day and the 38th week of 1976)
As you can see, you can format the date to quite some extend. But it isn't straightforward. The XSLT 2.0 team recognized this and many other problems with string parsing, types, functions etc and put them into the new XSLT 2.0 spec. All the above can be achieved in one line in XSLT 2.0. The best and most compliant XSLT 2.0 processor is currently Saxon: http://www.saxonica.com.

<xsl:stylesheet version="1.0"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:import href="parse-date.xslt" />
    <xsl:import href="format-date.xslt"/>
 
    <xsl:output method="text" />
 
    <xsl:param name="date" select="'16-09-1976'" />
 
    <xsl:variable name="isoDate">
        <xsl:call-template name="date:parse-date">
            <xsl:with-param name="date-time" select="$date" />
            <xsl:with-param name="format" select="'d-MM-yyyy'" />
        </xsl:call-template>
    </xsl:variable>
 
    <xsl:template match="/">
        <xsl:text>input date: </xsl:text>
        <xsl:value-of select="$date"/>
        <xsl:text>&#xa;iso date:   </xsl:text>
        <xsl:value-of select="$isoDate"/>
 
        <xsl:variable name="full-date">
            <xsl:call-template name="date:format-date">
                <xsl:with-param name="date-time" select="$isoDate" />
                <xsl:with-param name="pattern" select="'dd MMMM yyyy'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="day-of-year">
            <xsl:call-template name="date:format-date">
                <xsl:with-param name="date-time" select="$isoDate" />
                <xsl:with-param name="pattern" select="'D'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="week-of-year">
            <xsl:call-template name="date:format-date">
                <xsl:with-param name="date-time" select="$isoDate" />
                <xsl:with-param name="pattern" select="'w'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="year">
            <xsl:call-template name="date:format-date">
                <xsl:with-param name="date-time" select="$isoDate" />
                <xsl:with-param name="pattern" select="'yyyy'" />
            </xsl:call-template>
        </xsl:variable>
 
        <xsl:text>&#xa;full date:  </xsl:text>
        <xsl:value-of select="$full-date" />
        <xsl:text> (is the </xsl:text>
        <xsl:value-of select="$day-of-year" />
        <xsl:text>th day and the </xsl:text>
        <xsl:value-of select="$week-of-year" />
        <xsl:text>th week of </xsl:text>
        <xsl:value-of select="$year" />
        <xsl:text>)</xsl:text>
 
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

sharepointuser

ASKER
Please let me know where I can find date:parse-date template.
I wanted the xslt version and not exslt.

Thanks
abel

That is here (see also my earlier post): http://www.exslt.org/date/functions/format-date/date.format-date.template.xsl

That it is posted on the EXSLT website does not mean it uses EXSLT extension functions. Rather the opposite: it is pure XSLT 1.0.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
abel

Just for your information: EXSLT is (was, it is not very active anymore apart from client side implementations in Firefox and Opera) an effort to standardize a common set of extensions to XSLT 1.0 processors. Since XSLT provided for a method of extending the functions in a language, EXSLT suggested a common set of functions on many day-to-day tasks that should better be provided natively by the processor.

Some processors have since implemented these extensions, but unfortunately not in a very portable manner. Luckily, quite some people provided professionally designed templates where the task could be done by using pure XSLT 1.0 alone, by creating templates with the same name as the functions which could be called by using xsl:call-template.

This was also the intend of the EXSLT project: provide a set of ready made templates for day-to-day tasks but where the XSLT 1.0 language did not have an easy equivalent for. And one thing XSLT 1.0 is notoriously bad at is dealing with string and dates.
sharepointuser

ASKER
Could you please check that link.
I did not find the date:parse-date template.
I even searched the page for the string but I did not find it.
abel

OOOOOPSSS! A thousand apologies, I kept copying the same links again and again... Should be this: http://www.exslt.org/date/functions/parse-date/date.parse-date.template.xsl.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
sharepointuser

ASKER
<xsl:variable name="formattedDate">
        <xsl:call-template name="date:parse-date">
            <xsl:with-param name="date-time" select="eventstartdate" />
            <xsl:with-param name="format" select="'M/d/y'" />
        </xsl:call-template>
    </xsl:variable>
        <xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="$formattedDate" />
            <xsl:with-param name="pattern" select="'MMM dd yyyy'" />
    </xsl:call-template>

Now the first part is working fine i.e
<xsl:variable name="formattedDate">
        <xsl:call-template name="date:parse-date">
            <xsl:with-param name="date-time" select="eventstartdate" />
            <xsl:with-param name="format" select="'M/d/y'" />
        </xsl:call-template>
    </xsl:variable>
and I am getting the output as 2009-02-28
using
<xsl:value-of select="$formattedDate" />

But the main date transformation in the following code is not working:

<xsl:call-template name="date:format-date">
            <xsl:with-param name="date-time" select="$formattedDate" />
            <xsl:with-param name="pattern" select="'MMM dd yyyy'" />
    </xsl:call-template>
abel

> But the main date transformation in the following code is not working:

Can you tell me what "not working" means? Do you get different output then my example?
sharepointuser

ASKER
I am getting a generic error as

Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator.

In sharepoint web part it does not give details of where the error occured.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
abel

That's problematic. And you get that error only once you included that format-date call-template section? Or do you also get it if, say, you would manually put that formatted date in?
abel

We'll need to get the specific error behind that error. Can you follow the suggestions about viewing the Event Viewer and/or logging the errors here: http://www.sharepoint911.com/Lists/How%20Do%20I/DispForm.aspx?ID=1?
sharepointuser

ASKER
I am getting the error even if I manually put the formatted date in.
I am not using the SharePoint Server 2003 but using MOSS 2007
However, I checked the event logs and the event viewer, but I did not find any thing over there
Maybe the web part errors are not logged there.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
abel

Sounds to me that the error is not at all related to the XSLT formatting/processing then.I am not a sharepoint user, there's a sharepoint zone, you can try raising the question there...
sharepointuser

ASKER
ok
abel

Did you manage to get the formatting part working? Or aren't you sure you sure yet because you still have this sharepoint issue? Feel free to check back (even though you've accepted my solution) if you still need some help on the formatting date bit, once the sharepoint issue is out of the way.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sharepointuser

ASKER
There is an issue with the formatting.
I am not sure how to debug xslt in web parts.

Thanks
abel

The usual trick is, actually, to take the XSLT out completely and test them on their own.

But earlier, you reported genuine XSLT errors when using the XSLT stylesheets. If the issue is with the formatting, how do you use the output from the formatting, or how do you attach the stylesheets and/or call them?