文章来源:https://blog.csdn.net/yjs_lh/article/details/330911
XSL做日期格式转换的函数源代码如下:
<?xml version="1.0"encoding="gb2312"?> <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputencoding="gb2312"method="html"/> <!--日期格式转换 必须输入四位年,如果日期是两位,输出可能不正常--> <xsl:templatename="FORMATDATE"> <xsl:paramname="value"select="'2005.01.02 03:10:13'"/> <xsl:paramname="srcformat"select="'yyyy.MM.dd'"/> <xsl:paramname="desformat"select="'yyyy年MM月dd日'"/> <xsl:variablename="SrcFormat"> <xsl:choose> <xsl:whentest="string-length(srcformat) > 7"> <xsl:value-ofselect="srcformat"/> </xsl:when> <xsl:otherwise>yyyy.MM.dd</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variablename="DesFormat"> <xsl:choose> <xsl:whentest="string-length(desformat) > 7"> <xsl:value-ofselect="desformat"/> </xsl:when> <xsl:otherwise>yyyy年MM月dd日</xsl:otherwise> </xsl:choose> </xsl:variable> <!--分别获取年月日--> <xsl:variablename="year"> <xsl:choose> <xsl:whentest="string-length($DesFormat) - string-length(translate($DesFormat, 'y','')) > 4 "> <xsl:value-ofselect="number(substring($value, string-length(substring-before($SrcFormat, 'y')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'y',''))))"/> </xsl:when> <xsl:otherwise> <xsl:value-ofselect="substring($value, string-length(substring-before($SrcFormat, 'y')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'y','')))"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variablename="month"> <xsl:choose> <xsl:whentest="string-length($DesFormat) - string-length(translate($DesFormat, 'M','')) = 1 "> <xsl:value-ofselect="number(substring($value, string-length(substring-before($SrcFormat, 'M')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'M',''))))"/> </xsl:when> <xsl:otherwise> <xsl:value-ofselect="substring($value, string-length(substring-before($SrcFormat, 'M')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'M','')))"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variablename="day"> <xsl:choose> <xsl:whentest="string-length($DesFormat) - string-length(translate($DesFormat, 'M','')) = 1 "> <xsl:value-ofselect="number(substring($value, string-length(substring-before($SrcFormat, 'd')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'d',''))))"/> </xsl:when> <xsl:otherwise> <xsl:value-ofselect="substring($value, string-length(substring-before($SrcFormat, 'd')) + 1, string-length($SrcFormat) - string-length(translate($SrcFormat,'d','')))"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <!--分别替换年月日--> <xsl:variablename="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:variablename="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-ofselect="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:templatematch="/"> <html> <head> <title> 日期转换函数</title> </head> <body> <table border="1"> <caption>日期转换</caption> <tbody> <tr> <th>日期</th> <th>原格式</th> <th>新格式</th> <th>转换结果</th> </tr> <xsl:for-eachselect="//d"> <tr> <td> <xsl:value-ofselect="value"/> </td> <td> <xsl:value-ofselect="srcformat"/> </td> <td> <xsl:value-ofselect="desformat"/> </td> <td> <xsl:call-templatename="FORMATDATE"> <xsl:with-paramname="value"select="value"/> <xsl:with-paramname="srcformat"select="srcformat"/> <xsl:with-paramname="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日 |