4

I am trying to convert a date in xml that is in the format 11-APR-16, I would like to parse the date and convert it in 2016-04-11 or yyyy-mm-dd

I currently am using the Xslt code to convert the date:

<xsl:variable name="Date" select="//MEDIAITEMS_ROW/CREATED_DATE"/>
<xsl:variable name="Day" select="substring($Date,1,2)" />
<xsl:variable name="month" select="substring($Date,4,3)" />
<xsl:variable name="Year" select="substring($Date,8,2)" />
<xsl:template name="get-month-abbreviation">
        <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:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
        </xsl:choose>

</xsl:template>

However I am still returning the value APR, how should I go about converting the date from letters to number values

6
  • 1
    you might share some more context so we might see why this is doesn't work May 25, 2016 at 20:11
  • 1
    @StefanHegny I would think it is quite obvious what is wrong.
    – hr_117
    May 25, 2016 at 20:13
  • @hr_117 bang you sure are right - I apologize, should go to bed - except if there is still something else wrong ... May 25, 2016 at 20:20
  • Do I also need to use apply-template to get the values to read in, I am still getting the Letter values for the month and not the number
    – hguza
    May 25, 2016 at 20:26
  • You have a named template, but you don't seem to be calling it - at least not in the part of the code that you show us. May 25, 2016 at 20:27

3 Answers 3

3

Because you like to compare your variable with an string
change your xsl:when from e.g.:

<xsl:when test="$month = JAN">01</xsl:when>

to:

<xsl:when test="$month = 'JAN'">01</xsl:when>

test="$month = JAN"compares the variable $month with an element JAN in current node.

Update add working example for ARP (only)

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8"/>

  <xsl:variable name="Date" select="'11-APR-16'"/>
  <xsl:variable name="Day" select="substring($Date,1,2)" />
  <xsl:variable name="month" select="substring($Date,4,3)" />
  <xsl:variable name="Year" select="substring($Date,8,2)" />
  <xsl:template name="get-month-abbreviation">
    <xsl:choose>
         <xsl:when test="$month = 'APR'">04</xsl:when>
        <xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
      <xsl:call-template name="get-month-abbreviation" />
  </xsl:template>

</xsl:styles
3
  • Unfortunately the value of the month in APR form is still what is being read in my output. Am I formatting the way I call the variable month into the template incorrectly?
    – hguza
    May 25, 2016 at 20:18
  • Thank you would have taken me hours to see those problems
    – hguza
    May 25, 2016 at 20:44
  • Within the xsl:when element, you may need to output the result with xsl:value-of select="'04'" />
    – jtb
    May 26, 2016 at 8:42
3

Everyone seems to have been assuming that you are using XSLT 1.0, although you don't state this as a constraint, but it's worth knowing that there are lots of things in XSLT 2.0 that make this problem easier.

First and most obvious, you can use regular expressions:

<xsl:analyze-string select="$date" regex="([0-9]{2})\-([A-Z]{3})\-([0-9]{4})">
  <xsl:matching-substring>
    <xsl:value-of select="regex-group(3), f:month-num(regex-group(2)), regex-group(1)" separator="-"/>
  </xsl:matching-substring>
</xsl:analyze-string>

Secondly, you can use functions rather than templates:

<xsl:variable name="months" as="xs:string*" select="'JAN', 'FEB', 'MAR', ..."/>
<xsl:function name="f:month-num" as="xs:string">
  <xsl:param name="month-name" as="xs:string"/>
  <xsl:sequence select="format-number(index-of($months, $month-name), '00')"/>
</xsl:function>

One of the benefits of using functions is that the context doesn't leak into the function. So writing APR rather than 'APR' will give you an error message rather than trying to find a child element named APR. Another benefit is those "as" attributes - data can be type-checked on its way into and out of the function, and this tends to give you earlier and better error messages when you mess up.

1

An efficient XSLT 1.0 solution using keys:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:d="my:d">
 <xsl:output method="text"/>

 <d:Months>
   <month n="01">JAN</month>
   <month n="02">FEB</month>
   <month n="03">MAR</month>
   <month n="04">APR</month>
   <month n="05">MAY</month>
   <month n="06">JUN</month>
   <month n="07">JUL</month>
   <month n="08">AUG</month>
   <month n="09">SEP</month>
   <month n="10">OCT</month>
   <month n="11">NOV</month>
   <month n="12">DEC</month>
 </d:Months>

 <xsl:key name="kMonthByVal" match="month/@n" use=".."/>

  <xsl:template match="dt" name="funConvertDate">
    <xsl:param name="pstrDate" select="."/>

    <xsl:variable name="vMonthNum">
      <xsl:for-each select="document('')">
        <xsl:value-of select="key('kMonthByVal', substring($pstrDate, 4, 3))"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select=
        "concat('20', substring($pstrDate, 8, 2), '-', 
                $vMonthNum, '-', substring($pstrDate, 1, 2))"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<dt>11-APR-16</dt>

the wanted, correct result is produced:

2016-04-11

Do Note:

  1. The computational complexity of finding the numerical month representation is O(1) (good XSLT processors implement keys using hash-tables).

  2. No conditional instruction is used at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.