Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a xml which has date like this

XML
<tns:GoalsRequest>
    <tns:accountNumber>23456</tns:accountNumber>
    <tns:customerName>Smith</tns:customerName>
    <tns:customerDOB>22-AUG-1981</tns:customerDOB>
    <tns:searchBeginDate>13-DEC-1967</tns:searchBeginDate>
    <tns:searchEndDate>08-DEC-1968</tns:searchEndDate>
</tns:GoalsRequest>


Now I want to translate date to 1981-08-22

In my xsl I used
XML
<tns:customerDOB>
    <xsl:value-of select="format-date(bi:customerDOB,'[D01]-[M01]-[YYYY]')"/>
</tns:customerDOB>

But I am getting the following error

cast failed, invalid lexical value - xs:date'22-AUG-1981

Can anybody helpme to convert this date format

Thanks
Posted
Updated 24-Aug-12 12:11pm
v2

1 solution

Hello,

format-date formats a date into the date format of your choice.
see here : http://www.w3.org/2005/xpath-functions/[^]
and there:
http://www.w3.org/TR/xmlschema-2/#date[^]

Many parser will demand that the date you want to transform has a format like 2002-10-09T11:00:00, so 08-DEC-1968 will not be recognised.

A way to solve this is do it yourself and not use date-format, for example:
XML
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="*">
    <xsl:value-of select="substring(customerDOB,8,4)" />
    <xsl:variable name="month" select="substring(customerDOB,4,3)" />
    <xsl:choose>
      <xsl:when test="$month = 'JAN'">-01-</xsl:when>
      <xsl:when test="$month = 'FEB'">-02-</xsl:when>
      <xsl:when test="$month = 'MAR'">-03-</xsl:when>
      <xsl:when test="$month = 'APR'">-04-</xsl:when>
      <xsl:when test="$month = 'MAY'">-05-</xsl:when>
      <xsl:when test="$month = 'JUN'">-06-</xsl:when>
      <xsl:when test="$month = 'JUL'">-07-</xsl:when>
      <xsl:when test="$month = 'AUG'">-08-</xsl:when>
      <xsl:when test="$month = 'SEP'">-09-</xsl:when>
      <xsl:when test="$month = 'OCT'">-10-</xsl:when>
      <xsl:when test="$month = 'NOV'">-11-</xsl:when>
      <xsl:when test="$month = 'DEC'">-12-</xsl:when>
    </xsl:choose>
    <xsl:value-of select="substring(customerDOB,1,2)" />
  </xsl:template>
</xsl:stylesheet>


Hope it helps.

Valery.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900