2017-07-31 57 views
0

這裏的datetime屬性值是我的源XML:需要一個XSL來更新XML

<Chat> 
    <Chat StartTime="2017-05-28T02:05:52"> 
    <message userId="02A0592964A8F75F" timeShift="4"> 
     <msgText> Hi </msgText> 
    </message> 
    <message userId="123458566666666B" timeShift="30"> 
     <msgText> Hello.. How can I Help You. ? </msgText> 
    </message> 
</Chat> 

我需要一個XSL來trnsform來源爲:

<Chats> 
    <message time="2017-05-28T02:05:56" userId="02A0592964A8F75F">Hi</message> 
    <message time="2017-05-28T02:06:32" userId="02A0592964A8F75F">Hello.. How can I Help You. ?</message> 
</Chats> 

在輸出XML文件,應使用基於TimeShift(秒)的StartTime更新消息時間。

+0

你可以使用XSLT 2.0嗎? –

回答

0

如果可以使用XSLT 2.0,那麼你的變換會是這樣的:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" extension-element-prefixes="xs"> 
    <xsl:output indent="yes"/> 
    <xsl:template match="/"> 
    <Chats> 
     <xsl:variable name="timeShift" select="Chat/@StartTime"/> 
     <xsl:for-each select="Chat/message"> 
     <message> 
      <xsl:attribute name="time"> 
      <xsl:value-of select="xs:dateTime($timeShift) + xs:dayTimeDuration(concat('PT', @timeShift,'S'))"/> 
      </xsl:attribute> 
      <xsl:attribute name="userId"> 
      <xsl:value-of select="@userId"/> 
      </xsl:attribute> 
      <xsl:value-of select="msgText"/> 
     </message> 
     </xsl:for-each> 
    </Chats> 
    </xsl:template> 
</xsl:stylesheet> 

這讓下面的結果:

<Chats> 
    <message time="2017-05-28T02:05:56" userId="02A0592964A8F75F"> Hi </message> 
    <message time="2017-05-28T02:06:22" userId="123458566666666B"> Hello.. How can I Help You. ? </message> 
</Chats> 

這是從你的有點不同:

<Chats> 
    <message time="2017-05-28T02:05:56" userId="02A0592964A8F75F">Hi</message> 
    <message time="2017-05-28T02:06:32" userId="02A0592964A8F75F">Hello.. How can I Help You. ?</message> 
</Chats> 

所以我有一個問題。你是否需要從StartTime或之前的消息轉移?

+0

嗨....非常感謝您這麼快速的迴應但是,當我說XSTobject.Transform()時,它會拋出以下錯誤: {「找不到與命名空間'http關聯的腳本或擴展對象://www.w3.org/2001/XMLSchema'。「} –

+0

我需要從開始時間轉移........ 每個消息時間應根據時間轉換計算, 即Messagetime = StartTime + Timeshift (秒) –

+0

好吧,日期計算正確。至於錯誤,它看起來像它不支持version =「2.0」。如果你想使用2.0版本的.NET,那麼你需要使用第三方工具,不管是.NET版本的Saxon 9還是AltovaXML工具。也許重寫轉換使用版本1.0 –

相關問題