代码之家  ›  专栏  ›  技术社区  ›  bluegray

转换XSLT中的时间字符串

  •  2
  • bluegray  · 技术社区  · 14 年前

    如何转换时间字符串

    20101115083000 +0200
    

    2010-11-15 08:30:00 +0200
    

    使用XSLT?

    3 回复  |  直到 14 年前
        1
  •  10
  •   Peter Alexandr Nikitin    14 年前

    <xsl:template name="format-date-time">
        <xsl:param name="date" select="'%Y-%m-%dT%H:%M:%S%z'"/>
        <xsl:value-of select="substring($date, 9, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring($date, 6, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring($date, 1, 4)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="substring($date, 12, 2)"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="substring($date, 15, 2)"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="substring($date, 18, 2)"/>
    </xsl:template>
    

    <xsl:call-template name="format-date-time">
        <xsl:with-param name="date" select="./Startdate"/>
    </xsl:call-template>
    

        3
  •  2
  •   Dimitre Novatchev    14 年前

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:call-template name="convertDateTime">
       <xsl:with-param name="pDateTime" select="."/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat">
        <year offset="0" length="4"/>
        <month offset="4" length="2"/>
        <day offset="6" length="2"/>
        <hour offset="8" length="2"/>
        <minutes offset="10" length="2"/>
        <seconds offset="12" length="2"/>
        <zone offset="15" length="5"/>
      </xsl:param>
    
      <xsl:param name="pOutFormat">
       <y/>-<mo/>-<d/> <h/>:<m/>:<s/> <z/>
      </xsl:param>
    
      <xsl:variable name="vInFormat" select=
       "document('')/*/xsl:template[@name='convertDateTime']
                           /xsl:param[@name='pInFormat']"/>
      <xsl:variable name="vOutFormat" select=
       "document('')/*/xsl:template[@name='convertDateTime']
                           /xsl:param[@name='pOutFormat']"/>
      <xsl:apply-templates select="$vOutFormat/node()"
           mode="_convertDateTime">
        <xsl:with-param name="pDateTime" select="$pDateTime"/>
        <xsl:with-param name="pInFormat" select="$vInFormat"/>
       </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="y" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/year/@offset,
                    $pInFormat/year/@length)"/>
     </xsl:template>
    
     <xsl:template match="mo" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/month/@offset,
                    $pInFormat/month/@length)"/>
     </xsl:template>
    
     <xsl:template match="d" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/day/@offset,
                    $pInFormat/day/@length)"/>
     </xsl:template>
    
     <xsl:template match="h" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/hour/@offset,
                    $pInFormat/hour/@length)"/>
     </xsl:template>
    
     <xsl:template match="m" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/minutes/@offset,
                    $pInFormat/minutes/@length)"/>
     </xsl:template>
    
     <xsl:template match="s" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/seconds/@offset,
                    $pInFormat/seconds/@length)"/>
     </xsl:template>
    
     <xsl:template match="z" mode="_convertDateTime">
      <xsl:param name="pDateTime"/>
      <xsl:param name="pInFormat"/>
    
      <xsl:value-of select=
         "substring($pDateTime,
                    1+$pInFormat/zone/@offset,
                    $pInFormat/zone/@length)"/>
     </xsl:template>
    </xsl:stylesheet>
    

    <dateTime>20101115083000 +0200</dateTime>
    

       2010-11-15 08:30:00 +0200
    
    推荐文章