微秒到 xsl 中的可读日期时间

microsecond to readable datetime in xsl

我想使用 XSLT 1.0 样式表转换将 xml 文件中的日期和时间转换为人类可读的格式,例如 (2016-10-14),这是以下 xml 内容:

<?xml version="1.0" encoding="UTF-8"?>
<News DateTime="636120534151823750" Id="5241">
</News>

非常感谢,

这样试试:

<xsl:template match="News">
    <dateTime>
        <xsl:call-template name="datetime.ticks-to-datetime">
            <xsl:with-param name="datetime.ticks" select="@DateTime" />
        </xsl:call-template>
    </dateTime>
</xsl:template>

<xsl:template name="datetime.ticks-to-datetime">
    <xsl:param name="datetime.ticks"/>

    <xsl:variable name="JDN" select="floor($datetime.ticks div 864000000000) + 1721426" />
    <xsl:variable name="rem-ticks" select="$datetime.ticks mod 864000000000"/>

    <xsl:variable name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
    <xsl:variable name="e" select="4*$f + 3"/>
    <xsl:variable name="g" select="floor(($e mod 1461) div 4)"/>
    <xsl:variable name="h" select="5*$g + 2"/>

    <xsl:variable name="d" select="floor(($h mod 153) div 5 ) + 1"/>
    <xsl:variable name="m" select="(floor($h div 153) + 2) mod 12 + 1"/>
    <xsl:variable name="y" select="floor($e div 1461) - 4716 + floor((14 - $m) div 12)"/>

    <xsl:variable name="H" select="floor($rem-ticks div 36000000000)"/>
    <xsl:variable name="M" select="floor($rem-ticks mod 36000000000 div 600000000)"/>
    <xsl:variable name="S" select="$rem-ticks mod 600000000 div 10000000"/>

    <xsl:value-of select="format-number($y, '0000')" />    
    <xsl:value-of select="format-number($m, '-00')"/>
    <xsl:value-of select="format-number($d, '-00')"/>   

    <xsl:value-of select="format-number($H, ' 00')" />    
    <xsl:value-of select="format-number($M, ':00')"/>
    <xsl:value-of select="format-number($S, ':00')"/>   
</xsl:template>   

应用于您的输入示例,结果将是:

<dateTime>2016-10-14 14:50:15</dateTime>