2017-02-14 30 views
0

Я хотел бы отформатировать поле datetime, отображаемое в моем элементе заголовка. Это элемент startAt = "2016-11-03T08: 29: 13Z" в моем xml.Форматировать строковое поле datetime для удаления конечного символа «Z» и заменить символ «T» пробелом

Замените «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> 
+0

Сократите пример до минимума, необходимого для демонстрации проблема - см. [mcve]. –

ответ

0

Вы не можете форматировать e datetime "в 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