XSL做日期格式转换的函数,废话不多说,源代码如下:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="gb2312" method="html"/>
<!--
版权所有 叶建生 yjs_lh@sohu.com yjs_lh@sina.com
@version 1.0 2005.03.26 01:58 于重庆南坪
-->
<!--日期格式转换 必须输入四位年,如果日期是两位,输出可能不正常-->
<xsl:template name="FORMATDATE">
<xsl:param name="value" select="'2005.01.02 03:10:13'"/>
<xsl:param name="srcformat" select="'yyyy.MM.dd'"/>
<xsl:param name="desformat" select="'yyyy年MM月dd日'"/>
<xsl:variable name="SrcFormat">
<xsl:choose>
<xsl:when test="string-length(srcformat) > 7">
<xsl:value-of select="srcformat"/>
</xsl:when>
<xsl:otherwise>yyyy.MM.dd</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="DesFormat">
<xsl:choose>
<xsl:when test="string-length(desformat) > 7">
<xsl:value-of select="desformat"/>
</xsl:when>
<xsl:otherwise>yyyy年MM月dd日</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--分别获取年月日-->
<xsl:variable name="year">
<xsl:choose>
<xsl:when test="string-length($DesFormat) - string-length(translate($DesFormat, 'y','')) > 4 ">
<xsl:value-of select="number(substring($value, string-length(substring-before($SrcFormat, 'y')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'y',''))))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($value, string-length(substring-before($SrcFormat, 'y')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'y','')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="month">
<xsl:choose>
<xsl:when test="string-length($DesFormat) - string-length(translate($DesFormat, 'M','')) = 1 ">
<xsl:value-of select="number(substring($value, string-length(substring-before($SrcFormat, 'M')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'M',''))))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($value, string-length(substring-before($SrcFormat, 'M')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'M','')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="day">
<xsl:choose>
<xsl:when test="string-length($DesFormat) - string-length(translate($DesFormat, 'M','')) = 1 ">
<xsl:value-of select="number(substring($value, string-length(substring-before($SrcFormat, 'd')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'d',''))))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($value, string-length(substring-before($SrcFormat, 'd')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'d','')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--分别替换年月日-->
<xsl:variable name="d1" select="concat(substring-before($DesFormat, 'y'), $year, substring($DesFormat, string-length(substring-before($DesFormat, 'y')) + string-length($DesFormat) - string-length(translate($DesFormat, 'y','')) + 1 ))"/>
<xsl:variable name="d2" select="concat(substring-before($d1, 'M'), $month, substring($d1, string-length(substring-before($d1, 'M')) + string-length($d1) - string-length(translate($d1, 'M','')) + 1 ))"/>
<xsl:value-of select="concat(substring-before($d2, 'd'), $day, substring($d2, string-length(substring-before($d2, 'd')) + string-length($d2) - string-length(translate($d2, 'd','')) + 1))"/>
</xsl:template>
<!--用于测试-->
<xsl:template match="/">
<html>
<head>
<title> 日期转换函数 </title>
</head>
<body>
<table border="1">
<caption>日期转换</caption>
<tbody>
<tr>
<th>日期</th>
<th>原格式</th>
<th>新格式</th>
<th>转换结果</th>
</tr>
<xsl:for-each select="//d">
<tr>
<td>
<xsl:value-of select="value"/>
</td>
<td>
<xsl:value-of select="srcformat"/>
</td>
<td>
<xsl:value-of select="desformat"/>
</td>
<td>
<xsl:call-template name="FORMATDATE">
<xsl:with-param name="value" select="value"/>
<xsl:with-param name="srcformat" select="srcformat"/>
<xsl:with-param name="desformat" select="desformat"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
测试文件:
<?xml version="1.0" encoding="GB2312"?>
<Root>
<d>
<value>2005.12.01</value>
</d>
<d>
<value>2005.01.01</value>
</d>
<d>
<value>2005.12.31</value>
</d>
<d>
<value>2005.12.01 10:10:55</value>
</d>
<d>
<value>2005-01-01</value>
<srcformat>yyyy-MM-dd</srcformat>
<desformat>yyyy.MM.dd</desformat>
</d>
<d>
<value>2005-01-01</value>
<srcformat>yyyy-MM-dd</srcformat>
<desformat>yyyy.M.d</desformat>
</d>
<d>
<value>2005-01-01</value>
<srcformat>yyyy-MM-dd</srcformat>
<desformat>yyyy年M月d日</desformat>
</d>
<d>
<value>2005-01-01</value>
<srcformat>yyyy-MM-dd</srcformat>
<desformat>yyyy-M-d</desformat>
</d>
<d>
<value>2005-31-01</value>
<srcformat>yyyy-dd-MM</srcformat>
<desformat>yyyy-MM-dd</desformat>
</d>
<d>
<value>2005-31-01</value>
<srcformat>yyyy-dd-MM</srcformat>
</d>
<d>
<value>2005-31.01</value>
<srcformat>yyyy-dd.MM</srcformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
<desformat>yyyy.M.d</desformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
<desformat>yy.M.d</desformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
</d>
<d>
<value>01.31.2005</value>
<srcformat>MM.dd.yyyy</srcformat>
</d>
</Root>
输出结果:
日期 | 原格式 | 新格式 | 转换结果 |
---|---|---|---|
2005.12.01 | 2005年12月01日 | ||
2005.01.01 | 2005年01月01日 | ||
2005.12.31 | 2005年12月31日 | ||
2005.12.01 10:10:55 | 2005年12月01日 | ||
2005-01-01 | yyyy-MM-dd | yyyy.MM.dd | 2005.01.01 |
2005-01-01 | yyyy-MM-dd | yyyy.M.d | 2005.1.1 |
2005-01-01 | yyyy-MM-dd | yyyy年M月d日 | 2005年1月1日 |
2005-01-01 | yyyy-MM-dd | yyyy-M-d | 2005-1-1 |
2005-31-01 | yyyy-dd-MM | yyyy-MM-dd | 2005-01-31 |
2005-31-01 | yyyy-dd-MM | 2005年01月31日 | |
2005-31.01 | yyyy-dd.MM | 2005年01月31日 | |
01.31.2005 | MM.dd.yyyy | 2005年01月31日 | |
01.31.2005 | MM.dd.yyyy | yyyy.M.d | 2005.1.31 |
01.31.2005 | MM.dd.yyyy | yy.M.d | 2005年01月31日 |
01.31.2005 | MM.dd.yyyy | 2005年01月31日 | |
01.31.2005 | MM.dd.yyyy | 2005年01月31日 | |
01.31.2005 | MM.dd.yyyy | 2005年01月31日 |