Subject: Date Formatting
Posted By: MargateFan Post Date: 8/30/2006 6:48:23 AM
I'm trying to re-format a date and it's starting to drive me insane.
I've got the following XML tag:
<DateOfTravel>2006-08-29T00:00:00</DateOfTravel>

& need to convert it to:
<DepartureDate>29 August 2006</DepartureDate>

My XSLT currently has the following:
   <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



Reply By: mhkay Reply Date: 8/30/2006 6:55:21 AM
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
Reply By: joefawcett Reply Date: 8/30/2006 7:28:00 AM
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:
<xsl:value-of select="document('')/*/date:month-names/date:month[position() = $month]"/>


--

Joe (Microsoft MVP - XML)
Reply By: MargateFan Reply Date: 8/30/2006 7:42:52 AM
Cheers Micheal & Joe - it's working sound as a pound. I'd just changed it to
number(substring(DateOfTravel, 6, 2))
when Joe's relpy came in.


Go to topic 49061

Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180