How to format a time string as 12 or 24 hours using XSL 1.0?

时间:2015-11-12 11:44:07

标签: xml xslt time

I want to write into my XML file a time value in 24:00 format. It will be just the time value.

However, in the XSL file I would like to take this time value (eg: 17:00) and format it as 12 hour format if desired.

How do I do this?

Thank you.

Andrew

1 个答案:

答案 0 :(得分:3)

给出如下输入:

<time>15:35</time>

以下模板:

<xsl:template match="time">
    <xsl:copy>
        <xsl:variable name="h" select="substring-before(., ':')"/>
        <xsl:value-of select="($h + 11) mod 12 + 1"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="substring-after(.,':')"/>
        <xsl:value-of select="substring(' AM PM', 1 + 3*($h > 11), 3)"/>
    </xsl:copy>
</xsl:template>

将返回:

<time>3:35 PM</time>

如果您有多个要转换的时间节点,您可能希望将其转换为可以多次调用的命名模板。