设置日期时间字符串字段的格式以删除尾随的"Z"字符,并将"T"字符替换为空格



我想格式化我的标题元素中显示的日期时间字段。它是我的 xml 中的 startAt="2016-11-03T08:29:13Z" 元素。

将"T"替换为空格并删除"Z"。

这应该使用 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="@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: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:call-template name="format-duration">
                    <xsl:with-param name="value" select="@timeShift"/>
                </xsl:call-template>
            </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:call-template name="format-duration"><xsl:with-param name="value" select="@timeShift"/>
                </xsl:call-template>
            </label>
        </div>
    </xsl:template> 
        <xsl:template match="newParty[userInfo/@userType='EXTERNAL']">
            <div class="SystemJoined" id="{@userId}">
                <label>System: <xsl:value-of select="userInfo/@userNick" /></label>
                <label class="timeShiftLabel System">
                    <xsl:call-template name="format-duration">
                        <xsl:with-param name="value" select="@timeShift"/>
                    </xsl:call-template>
                </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:call-template name="format-duration">
                            <xsl:with-param name="value" select="@timeShift"/>
                        </xsl:call-template>
                    </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:call-template name="format-duration">
                            <xsl:with-param name="value" select="@timeShift"/>
                        </xsl:call-template>
                    </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:call-template name="format-duration">
                        <xsl:with-param name="value" select="@timeShift"/>
                    </xsl:call-template>
                </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:when test="$party-type='EXTERNAL'">System</xsl:when>
                </xsl:choose>
            </xsl:template>
        </xsl:stylesheet>

输入 xml 为:

<?xml version="1.0"?>
<chatTranscript startAt="2016-11-03T08:29:13Z" sessionId="0001KaC1NSN60019">
<newParty userId="0079581AF5590023" timeShift="0" visibility="ALL" eventId="1">
    <userInfo personId="" userNick="10.50.24.202" userType="CLIENT" protocolType="FLEX" timeZoneOffset="120"/>
    <userData>
        <item key="GMSServiceId">c0aa6221-d5f9-4fdc-9f75-ed63e99b1f12</item>
        <item key="IdentifyCreateContact">3</item><item key="MediaType">chat</item>
        <item key="TimeZone">120</item><item key="_data_id">139-9a8ee95b-d3ba-43a2-93a9-08ed7965d63d</item>
        <item key="firstName">Mike</item>
        <item key="lastName">Kumm</item>
    </userData>
</newParty>
<newParty userId="0079581AF56C0025" timeShift="20" visibility="ALL" eventId="2">
    <userInfo personId="1" userNick="allendei" userType="AGENT" protocolType="BASIC" timeZoneOffset="120"/>
</newParty>
<message userId="0079581AF56C0025" timeShift="32" visibility="ALL" eventId="4"><msgText treatAs="NORMAL">Hello</msgText></message>
<message userId="0079581AF5590023" timeShift="62" visibility="ALL" eventId="5"><msgText msgType="text" treatAs="NORMAL">Can you help me?</msgText></message>

预期结果是:

   <html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <link rel="stylesheet" type="text/css" href="/webrecall/css/chat.css">
   </head>
   <header>Chat - 0001KaC1NSN60019 - 2016-11-03 08:29:13</header>
   <div class="ClientJoined" id="0079581AF5590023"><label>Client: 10.50.24.202</label><label class="timeShiftLabel">00:00</label></div>
   <div class="AgentJoined" id="0079581AF56C0025"><label>Agent: allendei</label><label class="timeShiftLabel">00:20</label></div>
   <div class="Messages Agent" id="4"><label>Hello</label><label class="timeShiftLabel">01:32</label></div>
   <div class="Messages Client" id="5"><label>Can you help me?</label><label class="timeShiftLabel">01:02</label></div>
</html>

不能在 XSLT 1.0 中"设置日期时间字段的格式",因为 XSLT 1.0 没有日期、时间或日期时间的概念。所有这些在 XSLT 1.0 处理器中都显示为无意义的字符串。

但是,您可以使用 translate() 函数通过简单的字符串操作轻松实现所需的结果:

<xsl:value-of select="translate('2016-11-03T08:29:13Z', 'TZ', ' ')"/>

将返回:

2016-11-03 08:29:13

相关内容

  • 没有找到相关文章

最新更新