如何在xsl中将数字格式化为日期时间格式?

哈里特

这个问题是独特的,因为我看了两个相似的答案,而这些答案并不能解决我的问题。我希望将timeShift字段作为文本值而不是作为标记的属性添加到html中。

我有以下xml,并希望对timeShift字段进行一些时间处理,以使其采用以下格式:HH:MM:SS。

如何在xsl中执行此操作?

以下是我当前的xsl文件:

    <xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:strip-space elements="*"/>

<xsl:key name="party" match="newParty" use="@userId" />

<xsl:template match="/chatTranscript">
    <html>
        <head><link rel="stylesheet" type="text/css" href="/webrecall/css/chat.css"/></head>
        <header>Chat - <xsl:value-of select="@sessionId" /> - <xsl:value-of select="@startAt" /></header>
        <xsl:apply-templates/>
    </html>
</xsl:template>

<xsl:template match="newParty[userInfo/@userType='CLIENT']">
    <div class="ClientJoined" id="{@userId}">
        <label>Client: <xsl:value-of select="userInfo/@userNick" /></label>
        <label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>
      </div>
</xsl:template>

<xsl:template match="newParty[userInfo/@userType='AGENT']">
    <div class="AgentJoined" id="{@userId}">
        <label>Agent: <xsl:value-of select="userInfo/@userNick" /></label>
        <label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>
    </div>
</xsl:template> 

<xsl:template match="message">
    <xsl:variable name="party-class">
        <xsl:call-template name="lookup-class"/>
    </xsl:variable>
    <div class="Messages {$party-class}" id="{@eventId}">
        <label><xsl:value-of select="msgText" /></label>
        <label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>
    </div>
</xsl:template>   
<xsl:template match="notice">
    <xsl:variable name="party-class">
        <xsl:call-template name="lookup-class"/>
    </xsl:variable>
    <div class="Notices {$party-class}" id="{@eventId}">
        <label><xsl:value-of select="noticeText" /></label>
        <label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>
    </div>
</xsl:template>

<xsl:template match="partyLeft">
    <xsl:variable name="party-class">
        <xsl:call-template name="lookup-class"/>
    </xsl:variable>
    <div class="Notices {$party-class}" id="{@eventId}">
        <label><xsl:value-of select="reason" /></label>
        <label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>
    </div>
</xsl:template>

<xsl:template name="lookup-class">
    <xsl:variable name="party-type" select="key('party', @userId)/userInfo/@userType" />
    <xsl:choose>
        <xsl:when test="$party-type='CLIENT'">Client</xsl:when>
        <xsl:when test="$party-type='AGENT'">Agent</xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

以下是xml:

    <?xml version="1.0"?><chatTranscript startAt="2015-06-04T09:07:40Z" sessionId="0003CaANX11G00HD">
    <newParty userId="00955570155B0000" eventId="1" timeShift="0" visibility="ALL">
        <userInfo personId="" userNick="PhilC" userType="CLIENT" protocolType="FLEX" timeZoneOffset="120"/>
        <userData>
            <item key="EmailAddress"/>
            <item key="FirstName">Phil</item>
            <item key="IdentifyCreateContact">3</item>
            <item key="LastName">Collins</item>
            <item key="MediaType">chat</item>
        </userData>
    </newParty>
    <newParty userId="0095557015600002" eventId="2" timeShift="4" visibility="ALL">
        <userInfo personId="" userNick="system" userType="EXTERNAL" protocolType="ESP" timeZoneOffset="0"/>
    </newParty>
    <message userId="0095557015600002" eventId="3" timeShift="4" visibility="ALL">
        <msgText>agent will be with you shortly</msgText>
    </message>
    <newParty userId="00955570156E0003" eventId="4" timeShift="19" visibility="ALL">
        <userInfo personId="emailqa" userNick="emailqa" userType="AGENT" protocolType="BASIC" timeZoneOffset="120"/>
    </newParty>
    <message userId="00955570155B0000" eventId="6" timeShift="22" visibility="ALL">
        <msgText msgType="text" treatAs="NORMAL">hellO?</msgText>
    </message>
    <message userId="00955570156E0003" eventId="9" timeShift="26" visibility="ALL">
        <msgText treatAs="NORMAL">hi Phil</msgText>
    </message>
    <message userId="00955570156E0003" eventId="11" timeShift="28" visibility="ALL">
        <msgText treatAs="NORMAL">whatsup?</msgText>
    </message>
    <message userId="00955570155B0000" eventId="14" timeShift="45" visibility="ALL">
        <msgText msgType="text" treatAs="NORMAL">we're sitting next to each other but we're sending IMs  </msgText>
    </message>
    <message userId="00955570156E0003" eventId="17" timeShift="54" visibility="ALL">
        <msgText treatAs="NORMAL">hehehe</msgText>
    </message>
    <message userId="00955570156E0003" eventId="19" timeShift="56" visibility="ALL">
        <msgText treatAs="NORMAL">indeed</msgText>
    </message>
    <partyLeft userId="00955570156E0003" askerId="00955570156E0003" eventId="21" timeShift="77" visibility="ALL">
        <reason code="1">left with request to close if no agents</reason>
    </partyLeft>
    <partyLeft userId="00955570155B0000" askerId="00955570156E0003" eventId="22" timeShift="77" visibility="ALL">
        <reason code="4">removed by other party</reason>
    </partyLeft>
</chatTranscript>

因此,问题是如何将timeShift整数值作为输入(它表示经过的时间(以秒为单位)),并将其作为持续时间进行处理,格式为HH:mm:ss。

Valdi_Bo

熟悉以下功能的描述:

  • substring -获取字符串的一部分,
  • xs:time(timestring)-将字符串转换为xs:time类型,
  • format-time-从xs:time变量获取格式化的字符串

上面的功能可能特别有用,尤其是当您打算对时间值进行一些计算时。

另一种解决方案是使用substring函数从源字符串(例如,小时,分钟和第二部分)中提取所需的子字符串然后,您可以使用concat函数创建输出字符串

截至2017年2月13日编辑

我创建了以下示例。

<?xml version="1.0" encoding="UTF-8"?>
<Main>
  <Record timeShift="17" cause="A1">Abcd efgh ijlk</Record>
  <Record timeShift="85" cause="A2">Mnop qrst uvw xyz</Record>
</Main>

下面有一个示例XSL将timeShift属性转换mm:ss格式。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>

  <xsl:template match="@timeShift">
    <xsl:attribute name="timeShift">
      <xsl:call-template name="format-duration"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template name="format-duration">
    <xsl:param name="value" select="." />
    <xsl:param name="alwaysIncludeHours" select="false()" />
    <xsl:param name="includeSeconds" select="true()" />

    <xsl:if test="$value > 3600 or $alwaysIncludeHours">
      <xsl:value-of select="concat(format-number($value div 3600, '00'), ':')"/>
    </xsl:if>
    <xsl:value-of select="format-number($value div 60 mod 60, '00')" />
    <xsl:if test="$includeSeconds">
      <xsl:value-of select="concat(':', format-number($value mod 60, '00'))" />
    </xsl:if>
  </xsl:template>  

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

format-duration模板在XSLTSeconds到Time复制,对此帖子的评论中提到。

回到您的XSL来源:更改每种情况

<label class="timeShiftLabel"><xsl:value-of select="@timeShift" /></label>

至:

<label class="timeShiftLabel">
  <xsl:call-template name="format-duration">
    <xsl:with-param name="value" select="@timeShift"/>
    <xsl:with-param name="alwaysIncludeHours" select="true()" />
  </xsl:call-template>
</label>

当然,由于调用format-duration模板,因此也应将其包括在XSL中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

您如何在Dart中将月份的日期格式化为“ 11日”,“ 21日”或“ 23日”?

来自分类Dev

如何在Haskell中将UTCTime格式化为ISO 8601

来自分类Dev

如何在POSIXct日期中将'day'格式化为一位数字

来自分类Dev

在PowerShell中将日期时间格式化为\ / Date(1411704000000)\ /

来自分类Dev

如何在PHP中将数字300格式化为3.0

来自分类Dev

如何在SQL Server中将日期时间格式化为M / D / YYYY?

来自分类Dev

如何在Java,Android中将SQL日期格式化为简单日期格式?

来自分类Dev

如何在JasperSoft Studio中将数字格式化为2.564.894.621?

来自分类Dev

在Python中将数字格式化为货币

来自分类Dev

您如何在python中将203045格式化为这样的时间?

来自分类Dev

如何在r的逻辑回归方法中将p值格式化为4位数字

来自分类Dev

如何在Django中将数字从1000格式化为1k

来自分类Dev

如何在Python中将mysql日期时间yyyy-mm-dd格式化为dd-mm-yyyy?

来自分类Dev

如何在Google Apps脚本中将时间戳“ new Date()”格式化为新加坡时区?

来自分类Dev

如何在PHPExcel中将单元格值格式化为日期?

来自分类Dev

如何在jstl中将数字格式化为正值或绝对值?

来自分类Dev

如何在JavaScript中将日期字符串格式化为D,MMM yyyy

来自分类Dev

如何在Java,Android中将SQL日期格式化为简单日期格式?

来自分类Dev

如何在PHP中将日期格式化为意大利语方式?

来自分类Dev

如何在javascript中将字符串/数字格式化为自定义模式?

来自分类Dev

在Python中将数字格式化为货币

来自分类Dev

您如何在python中将203045格式化为这样的时间?

来自分类Dev

如何在 Ionic 2 中将 MySQL 日期时间格式化为 angular2 可用日期格式?

来自分类Dev

如何在 SQL 中将所选日期列格式化为相对时间格式

来自分类Dev

如何在 Java 中将日期格式化为带有缩写时区的格式?

来自分类Dev

如何在javascript中将日期格式化为“E, d MMM yyyy HH:mm:ss zzz”?

来自分类Dev

如何在 logback 中将日期格式化为纪元时间戳?

来自分类Dev

如何在 amcharts4 中将时间戳 unix 格式化为日期

来自分类Dev

在 Swift 中将日期格式化为 sql 日期格式

Related 相关文章

  1. 1

    您如何在Dart中将月份的日期格式化为“ 11日”,“ 21日”或“ 23日”?

  2. 2

    如何在Haskell中将UTCTime格式化为ISO 8601

  3. 3

    如何在POSIXct日期中将'day'格式化为一位数字

  4. 4

    在PowerShell中将日期时间格式化为\ / Date(1411704000000)\ /

  5. 5

    如何在PHP中将数字300格式化为3.0

  6. 6

    如何在SQL Server中将日期时间格式化为M / D / YYYY?

  7. 7

    如何在Java,Android中将SQL日期格式化为简单日期格式?

  8. 8

    如何在JasperSoft Studio中将数字格式化为2.564.894.621?

  9. 9

    在Python中将数字格式化为货币

  10. 10

    您如何在python中将203045格式化为这样的时间?

  11. 11

    如何在r的逻辑回归方法中将p值格式化为4位数字

  12. 12

    如何在Django中将数字从1000格式化为1k

  13. 13

    如何在Python中将mysql日期时间yyyy-mm-dd格式化为dd-mm-yyyy?

  14. 14

    如何在Google Apps脚本中将时间戳“ new Date()”格式化为新加坡时区?

  15. 15

    如何在PHPExcel中将单元格值格式化为日期?

  16. 16

    如何在jstl中将数字格式化为正值或绝对值?

  17. 17

    如何在JavaScript中将日期字符串格式化为D,MMM yyyy

  18. 18

    如何在Java,Android中将SQL日期格式化为简单日期格式?

  19. 19

    如何在PHP中将日期格式化为意大利语方式?

  20. 20

    如何在javascript中将字符串/数字格式化为自定义模式?

  21. 21

    在Python中将数字格式化为货币

  22. 22

    您如何在python中将203045格式化为这样的时间?

  23. 23

    如何在 Ionic 2 中将 MySQL 日期时间格式化为 angular2 可用日期格式?

  24. 24

    如何在 SQL 中将所选日期列格式化为相对时间格式

  25. 25

    如何在 Java 中将日期格式化为带有缩写时区的格式?

  26. 26

    如何在javascript中将日期格式化为“E, d MMM yyyy HH:mm:ss zzz”?

  27. 27

    如何在 logback 中将日期格式化为纪元时间戳?

  28. 28

    如何在 amcharts4 中将时间戳 unix 格式化为日期

  29. 29

    在 Swift 中将日期格式化为 sql 日期格式

热门标签

归档