Convert dd/mm/yy to dd-MM-YYYY format in xsl

2019-09-05 06:25发布

问题:

Thanks in advance for your support.

I have the date formats as dd/MMM/YY and need to be converted into the another format dd-MM-YYYY using XSL.

The problem here is, year is in short format need to be converted into long format. How can the year be determined ?

Here are few examples,

dd/MMM/YY - dd-MM-YYYY

01/FEB/91 - 01-02-1991.

13/APR/13 - 13-04-2013.

Using string manipulations,XSL it can be converted to long format but actual year cannot be determined.

For example

01/FEB/91 can be converted to 01-02-1991 or 01-02-2091 using string functions.

So, how to get the actual date using XSL 1.0 or XSL 2.0 ?

回答1:

You need to think about what you're asking. XSLT has no way of knowing whether 01/FEB/91 means 01-02-1991 or 01-02-2091 or 01-02-1891. You need to decide where the cutoff year is.

You could make the cutoff 2013 and do this:

<xsl:variable name="longYear"
              select="$shortYear + 2000 - (100 * ($shortYear > 13))" />

This will result in 20XX for years less than or equal to 13, and 19XX for years greater than 13.

In its own template:

<xsl:template name="LongYear">
  <xsl:param name="shortYear" select="." />
  <xsl:param name="cutoff" select="13" />

  <xsl:value-of select="$shortYear + 2000 - (100 * ($shortYear > $cutoff))" />
</xsl:template>

Which you can invoke with:

<xsl:call-template name="LongYear">
  <xsl:with-param name="shortYear" select="substring(Date, 8, 2)" />
</xsl:call-template>


回答2:

You can use customized java function in xslt as this date conversion is possible in Java for the same.