Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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
  #1 (permalink)  
Old June 21st, 2005, 10:49 AM
Authorized User
Join Date: May 2005
Location: Waterlooville, HANTS, United Kingdom.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default formatting strings / dates

Joe I need your help m8, google is not being very cooperative at all!

I have this xml:

   <dateTime value="6/June/2005 16:6:33" />

Using an .xslt stylesheet how on earth can I format that string so it
gets transformed to 6 Jun 2005?

I've tried:

<xsl:template match="QuoteTime/@value">
   <xsl:value-of select="format-date-time(., '%Y-%m-%dT%H:%M:%S%z')" />


<xsl:template match="//instruction/Qtn_Tbl/QuoteTime">
   <xsl:value-of select="dt:format-date(@value, 'dd MMM yyyy')" />

etc etc - its not working!

I've tried various function calls like date() date-format() etc but it appears to me that they all work on .xsl and not .xslt!

I've been on Microsoft to have a look at their examples but they all don't appear to be working due to a namespace descrepancy!


I have to admit to be simply creating these files in notepad and testing the stylesheet by opening them in IE rather using the Transform object in .Net...is this half of my problem or do I need to use .Net to transform the xml?

Why is .xsl so different to .xslt anyway! It's amazing, you go to the code downloads and flick through to Professional XSLT 2.0 downloads here and you will notice that every single stylesheet file is a .xsl and not a .xslt!

Am I missing something here?

  #2 (permalink)  
Old June 21st, 2005, 10:59 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

You're confused about language dialects.

XSLT 1.0 was defined by W3C in 1999. During its development it was often referred to as XSL, and this name survives in the conventional prefix xsl:stylesheet and the conventional file extension style.xsl.

Before XSLT 1.0 came out, Microsoft produced a product based on a working draft of the spec, with many of their own extensions. Older Microsoft documents refer to this obsolete dialect as XSL, but most people call it WD-xsl, after the namespace used to identify it. THis dialect is essentially dead.

XSLT 2.0 is still in draft, and not supported by any Microsoft products. It includes functions such as format-dateTime().

XSLT 1.0 allows the function library to be extended in a vendor-specific namespace. The MSXML4 product includes some such extensions, specifically for date and time formatting.

There are also some extension functions for date and time handling in the EXSLT library, see www.exslt.org.

Hope this clarifies!

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old June 22nd, 2005, 04:46 AM
Authorized User
Join Date: May 2005
Location: Waterlooville, HANTS, United Kingdom.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

To be honest...it's all a little bit confusing to me...xsl seems like such a big thing with so much promise that I want to dig deep and get as much out of this as possible but I seem to travel from one site to another trying to find answers but half the time I end up with error messages like not a valid XSLT pr XPath function!

There just seems to be so much ... I don't know..its all very cloudy to me at the moment but I guess its new to me that's all!

  #4 (permalink)  
Old June 22nd, 2005, 09:52 AM
Authorized User
Join Date: May 2005
Location: Waterlooville, HANTS, United Kingdom.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

Okay so after another load of searching I came across a page with a list of functions that did seem to work in xslt!

Below is the result of my work - If by any chance you know of a better way then please let me know. By the way I found that if I started a new value on another line it entered a space in hence the reason why it isn't so easy to read due to some of the closing tags being quickly followed by an opening tag!

Personally, I'm pretty happy with my effort...but it did take me some time!

the value being taken from the node is: - 6/June/2005 16:6:33
The value I want to show at the end is: - 6 June 2005 16:06

<xsl:template match="//someTime">
   <xsl:param name="dayDate" select="substring-before(@value, '/')" />
   <xsl:param name="afterDay" select="substring-after(@value, '/')" />
   <xsl:param name="monthDate" select="substring-before($afterDay, '/')" />
   <xsl:param name="afterMonth" select="substring-after($afterDay, '/')" />
   <xsl:param name="yearDate" select="substring-before($afterMonth, ' ')" />
   <xsl:param name="timeDate" select="substring-after($afterMonth, ' ')" />
   <xsl:param name="hourDate" select="substring-before($timeDate, ':')" />
   <xsl:param name="afterHour" select="substring-after($timeDate, ':')" />
   <xsl:param name="minuteDate" select="substring-before($afterHour, ':')" />

   <xsl:if test="string-length($dayDate) = 2" >
    <xsl:value-of select="$dayDate" />
   <xsl:if test="string-length($dayDate) = 1" >
    0<xsl:value-of select="$dayDate" />

    <xsl:when test="string-length($monthDate) > 4" >
          <xsl:value-of select="substring($monthDate, 1, 3)" />
      <xsl:value-of select="$monthDate" />
   #160;<xsl:value-of select="$yearDate" />

    <xsl:when test="string-length($hourDate) = 2" >
          <xsl:value-of select="$hourDate" />
      0<xsl:value-of select="$hourDate" />
   </xsl:choose>:<xsl:if test="string-length($minuteDate) = 2" >
    <xsl:value-of select="$minuteDate" />
   </xsl:if><xsl:if test="string-length($minuteDate) = 1" >0<xsl:value-of select="$minuteDate" /></xsl:if>


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Dates dhoward SQL Language 4 January 30th, 2007 04:00 PM
Formatting For Between Dates hcweb Classic ASP Basics 7 February 8th, 2005 02:29 AM
formatting dates treasacrowe Classic ASP Basics 3 October 20th, 2004 06:52 PM
Formatting dates in XSL francislang XSLT 4 September 3rd, 2004 08:17 AM
Formatting Dates Louisa VB.NET 2002/2003 Basics 3 March 16th, 2004 05:52 AM

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