Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT 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
 
Old August 30th, 2006, 06:48 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date Formatting

I'm trying to re-format a date and it's starting to drive me insane.
I've got the following XML tag:
Code:
<DateOfTravel>2006-08-29T00:00:00</DateOfTravel>
& need to convert it to:
Code:
<DepartureDate>29 August 2006</DepartureDate>
My XSLT currently has the following:
Code:
   <xsl:element name="DepartureDate">
     <xsl:value-of select="concat(substring(DateOfTravel, 9, 2), ' ')"/>
     <xsl:variable name="month" select="substring(DateOfTravel, 6, 2)"></xsl:variable>
     <xsl:call-template name="date:month-name">
       <xsl:with-param name="month" select="format-number($month, '###')"/>
     </xsl:call-template>
     <xsl:value-of select="concat(' ', substring(DateOfTravel, 1, 4))"/>
   </xsl:element>
.
.
.
.
.
  <date:month-names>
    <date:month>January</date:month>
    <date:month>February</date:month>
    <date:month>March</date:month>
    <date:month>April</date:month>
    <date:month>May</date:month>
    <date:month>June</date:month>
    <date:month>July</date:month>
    <date:month>August</date:month>
    <date:month>September</date:month>
    <date:month>October</date:month>
    <date:month>November</date:month>
    <date:month>December</date:month>
  </date:month-names>

  <xsl:template name="date:month-name">
    <xsl:param name="month" select="0"/>
    <xsl:value-of select="document('')/*/date:month-names/date:month[$month]"/>
  </xsl:template>
I've tried it without the <xsl:variable name="month" select="substring(DateOfTravel, 6, 2)"></xsl:variable> & just using the substring(DateOfTravel, 6, 2) in the with-param, plus I've tried it without the format-number as well - all seem to give me January as the month.
However if I just 'hard-code' a number in the with-param it works OK, which seems to imply that the "date:month-name" template is OK, it's the parameter that isn't, but running it in debug the parameter is '08'

I'm sure it's something straight-forward, help please

as always - thanks in advance
 
Old August 30th, 2006, 06:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The meaning of a predicate, like $month in

document('')/*/date:month-names/date:month[$month]

depends on whether it's a number or something else. In your case it's something else - a string - because for some reason you decided to put the value through format-number(). When you use a string as a predicate, values are selected if the string is non-zero-length, and are omitted otherwise.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 30th, 2006, 07:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

To add to Michael's reply you can fix by either making sure that $month is numeric, wrapping it in number() for example. Alternatively you can force the issue in the template:
Code:
<xsl:value-of select="document('')/*/date:month-names/date:month[position() = $month]"/>
--

Joe (Microsoft MVP - XML)
 
Old August 30th, 2006, 07:42 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers Micheal & Joe - it's working sound as a pound. I'd just changed it to
Code:
number(substring(DateOfTravel, 6, 2))
when Joe's relpy came in.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Formatting, need some assistance groovedrm Access VBA 2 June 16th, 2006 07:23 AM
Date formatting jroxit Classic ASP Basics 3 February 26th, 2006 10:07 PM
Date Formatting ozPATT Excel VBA 2 October 31st, 2005 12:03 PM
formatting date time Adam H-W Classic ASP Basics 4 September 29th, 2004 03:35 AM
Formatting to a Date trekmp ADO.NET 2 March 16th, 2004 01:37 PM





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