2

我有一个使用 XSL 转换的 XML 提要。XML 中每个帖子的日期采用以下格式:

2011-03-09T10:44:27Z

我希望能够将其转换为“50 分钟前”或“3 天前”格式,这可能仅使用 XSL 还是 PHP 是“唯一”选项?

4

1 回答 1

7

在 XSLT 1.0 中,使用 Jenny Tenison的 EXSLT 的纯 XSLT实现date:difference()

作为概念证明,这个样式表:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:date="http://exslt.org/dates-and-times">
    <xsl:import href="date.difference.template.xsl"/>
    <xsl:template match="date">
        <xsl:variable name="vDuration">
            <xsl:call-template name="date:difference">
                <xsl:with-param name="start" select="/test/@today" />
                <xsl:with-param name="end" select="." />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="vDays" 
             select="substring-before(substring-after($vDuration,'P'),'D')"/>
        <xsl:variable name="vHours" 
             select="substring-before(substring-after($vDuration,'T'),'H')"/>
        <xsl:variable name="vMinutes" 
             select="substring-before(substring-after($vDuration,'H'),'M')"/>
        <xsl:if test="$vDays">
            <xsl:value-of select="concat($vDays,' days ')"/>
        </xsl:if>
        <xsl:if test="$vHours">
            <xsl:value-of select="concat($vHours,' hours ')"/>
        </xsl:if>
        <xsl:if test="$vMinutes">
            <xsl:value-of select="concat($vMinutes,' minutes ')"/>
        </xsl:if>
        <xsl:text>ago&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<test today="2011-03-09T15:00:00Z">
    <date>2011-03-09T10:44:27Z</date>
    <date>2011-02-09T10:44:27Z</date>
</test>

输出:

4 hours 15 minutes ago
28 days 4 hours 15 minutes ago
于 2011-03-09T18:09:53.113 回答