Xml 格式化datetime字符串字段以删除尾随的';Z';字符,并替换';T';带空格的字符

Xml 格式化datetime字符串字段以删除尾随的';Z';字符,并替换';T';带空格的字符,xml,xslt-1.0,datetime-format,Xml,Xslt 1.0,Datetime Format,我想格式化标题元素中显示的datetime字段。它是我的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="

我想格式化标题元素中显示的datetime字段。它是我的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>

c0aa6221-d5f9-4fdc-9f75-ed63e99b1f12
3帽子
120139-9a8ee95b-d3ba-43a2-93a9-08ed7965d63d
迈克
库姆
你好
你能帮助我吗?

预期结果是:

   <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>

聊天室-0001KaC1NSN60019-2016-11-03 08:29:13
客户:10.50.24.20200:00
代理人:阿连迪00:20
你好,01:32
你能帮我吗?01:02
您不能在XSLT1.0中“格式化日期时间字段”,因为XSLT1.0没有日期、时间或日期时间的概念。所有这些对于XSLT1.0处理器来说都是毫无意义的字符串

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

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

请将示例减少到演示问题所需的最小值-请参阅。
2016-11-03 08:29:13