1

I have a problem that I am not really sure about how to go about solving. I need to convert the date format of the DateOfBirth node in the following XML from DD-MMM-YYYY to YYYY-MM-DD. I am aware over the format-date function in XSL but I'm not sure how this works when it comes to translating the months which are formatted as the text MMM format rather than the numerical MM format.

Any advice greatly appreciated.

<PersonalInformation>
  <Talisma_StudentID>987654</Talisma_StudentID>
  <SITS_StudentCode>201561230</SITS_StudentCode>
  <Title>MR</Title>
  <ForeName1>Charles</ForeName1>
  <ForeName2>XML</ForeName2>
  <ForeName3>TEST</ForeName3>
  <Surname>Dickens</Surname>
  <KnownAs>Charlie</KnownAs>
  <DateOfBirth>01-OCT-1950</DateOfBirth>
  <Sex>M</Sex>
</PersonalInformation>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Andy Kaufman
  • 761
  • 3
  • 9
  • 21

1 Answers1

4

This should do the job.

Format: DD-MMM-YYYY becomes YYYY-MM-DD
Example: 27-AUG-1987 becomes 1987-08-27

Using this XSL v1.0:

<xsl:template match="/">
  <html lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>Date Conversion</title>
    </head>
    <body>
      <xsl:call-template name="date">
        <xsl:with-param name="dd-mmm-yyyy" select="$XMLDOC/PersonalInformation/DateOfBirth"/>
      </xsl:call-template>
    </body>
  </html>
</xsl:template>

<xsl:template name="date">
  <xsl:param name="dd-mmm-yyyy"/>

  <xsl:variable name="dd" select="substring-before($dd-mmm-yyyy, '-')"/>
  <xsl:variable name="mmm-yyyy" select="substring-after($dd-mmm-yyyy, '-')"/>
  <xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
  <xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>

  <xsl:value-of select="$yyyy"/>
  <xsl:text>-</xsl:text>

  <xsl:choose>
    <xsl:when test="$mmm = 'JAN'">01</xsl:when>
    <xsl:when test="$mmm = 'FEB'">02</xsl:when>
    <xsl:when test="$mmm = 'MAR'">03</xsl:when>
    <xsl:when test="$mmm = 'APR'">04</xsl:when>
    <xsl:when test="$mmm = 'MAY'">05</xsl:when>
    <xsl:when test="$mmm = 'JUN'">06</xsl:when>
    <xsl:when test="$mmm = 'JUL'">07</xsl:when>
    <xsl:when test="$mmm = 'AUG'">08</xsl:when>
    <xsl:when test="$mmm = 'SEP'">09</xsl:when>
    <xsl:when test="$mmm = 'OCT'">10</xsl:when>
    <xsl:when test="$mmm = 'NOV'">11</xsl:when>
    <xsl:when test="$mmm = 'DEC'">12</xsl:when>
  </xsl:choose>

  <xsl:text>-</xsl:text>
  <xsl:value-of select="$dd"/>
</xsl:template>

Applied to this XML:

<PersonalInformation>
  <Talisma_StudentID>987654</Talisma_StudentID>
  <SITS_StudentCode>201561230</SITS_StudentCode>
  <Title>MR</Title>
  <ForeName1>Charles</ForeName1>
  <ForeName2>XML</ForeName2>
  <ForeName3>TEST</ForeName3>
  <Surname>Dickens</Surname>
  <KnownAs>Charlie</KnownAs>
  <DateOfBirth>01-OCT-1950</DateOfBirth>
  <Sex>M</Sex>
</PersonalInformation>

Produces this outcome:
1950-10-01

misterbear
  • 803
  • 2
  • 13
  • 33