XSLT 1.0格式时间

问题描述 投票:2回答:1

我正在使用XSLT 1.0,我有一个时间值存储为军事时间的整数,我需要将其输出到标准时间。例如,值为1400,我需要将其输出到下午2:00。这可以在XSLT 1.0中实现吗?

xml xslt xslt-1.0
1个回答
3
投票

这不仅仅是格式化。您还希望将24小时表示法转换为12小时表示法。

给出一个输入:

XML

<input>1435</input>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <output>
        <xsl:variable name="h" select="input div 100"/>
        <xsl:variable name="m" select="input mod 100"/>

        <xsl:variable name="h12" select="round(($h + 11) mod 12 + 1)"/>
        <xsl:variable name="am.pm" select="substring('AMPM', 1 + 2*($h > 11), 2)"/>

        <xsl:value-of select="$h12"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="format-number($m, '00')"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$am.pm"/>
    </output>
</xsl:template>

</xsl:stylesheet>

将返回:

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>2:35 PM</output>
© www.soinside.com 2019 - 2024. All rights reserved.