1. 程式人生 > >用XSL進行日期格式轉換

用XSL進行日期格式轉換

文章來源: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-of

select="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

20051201

2005.01.01

20050101

2005.12.31

20051231

2005.12.01 10:10:55

20051201

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

yyyyMd

200511

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

20050131

2005-31.01

yyyy-dd.MM

20050131

01.31.2005

MM.dd.yyyy

20050131

01.31.2005

MM.dd.yyyy

yyyy.M.d

2005.1.31

01.31.2005

MM.dd.yyyy

yy.M.d

20050131

01.31.2005

MM.dd.yyyy

20050131

01.31.2005

MM.dd.yyyy

20050131

01.31.2005

MM.dd.yyyy

20050131