我如何在XSL中进行时间格式化

时间:2014-04-10 18:38:09

标签: sql xml xslt

我想在XLS中将数字格式化为HH:mm。主要问题是输入数字是810( - > 8:10)和1530( - > 15:30) 我该怎么办?

我试着用这个:

<xsl:choose>
    <xsl:when test="string-length(IND)=4">
        <!--<td><xsl:value-of select="IND"></xsl:value-of></td>-->
        <td><xsl:value-of select="format-time(IND, 'HH:mm')"/></td>
    </xsl:when>   ...

但它丢了一个错误: XML-22018 :(错误)格式 - 时间函数中的解析错误。

4 个答案:

答案 0 :(得分:1)

尝试:

<xsl:value-of select="translate(format-number(IND div 100,'00.00'), '.', ':')"/>

答案 1 :(得分:0)

这个怎么样:

<xsl:variable name="hrLen" select="string-length(IND) - 2" />
<td><xsl:value-of select="concat(substring(IND, 1, $hrLen),
                                 ':',
                                 substring(IND, $hrLen + 1))" />

答案 2 :(得分:0)

如果使用Microsoft的XSL引擎,请尝试此操作:

<xsl:template match="Time">
  <xsl:copy>
    <xsl:value-of select="format-number(text(),'00:00')"/>
  </xsl:copy>
</xsl:template>

或者对于包括MS在内的其他XSL引擎(推荐):

<xsl:template name="FormatTime">
  <xsl:param name="numericTime" select="0" />
  <xsl:variable name="fourDigitString" select="format-number($numericTime,'0000')"/>
  <xsl:value-of select="concat(substring($fourDigitString,1,2),':',substring($fourDigitString,3,2))"/>
</xsl:template>

完整示例:

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Time">
    <xsl:text>&#10;</xsl:text>
    <xsl:element name="Universal">
      <xsl:copy>
        <xsl:call-template name="FormatTime">
          <xsl:with-param name="numericTime" select="text()" />
        </xsl:call-template>
      </xsl:copy>
    </xsl:element>
    <xsl:text>&#10;</xsl:text>
    <xsl:element name="MicrosoftAndMaybeOthers">
      <xsl:copy>
        <xsl:value-of select="format-number(text(),'00:00')"/>
      </xsl:copy>
    </xsl:element>
    <xsl:text>&#10;</xsl:text>

  </xsl:template>
  <xsl:template name="FormatTime">
    <xsl:param name="numericTime" select="0" />
    <xsl:variable name="fourDigitString" select="format-number($numericTime,'0000')"/>
    <xsl:value-of select="concat(substring($fourDigitString,1,2),':',substring($fourDigitString,3,2))"/>
  </xsl:template>


</xsl:stylesheet>

XML

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Time>810</Time>
  <Time>1538</Time>
</Root>

有关该功能的更多信息:http://www.w3schools.com/xsl/func_formatnumber.asp

答案 3 :(得分:0)

你正在调用format-time()。这是一个XSLT 2.0函数。您使用的是XSLT 2.0处理器吗? - 你没有说,奇怪的是,没有人问过。

如果您使用的是XSLT 2.0,则可以将此功能称为

format-time($time, '[H01]:[m01]')

但是,首先必须将$ time构造为xs:time值。从数字810开始,您必须执行xs:time('00:00:00') + ((($n idiv 100) * 60) + ($n mod 100))*xs:dayTimeDuration('PT1M') - 这只是为了插入冒号而有点用。

所以我认为我首选的2.0解决方案可能是

replace(string($N), '(\d\d)$', ':$1')
相关问题