2011-12-29 118 views
2

我有一個標準ISO 8601格式的日期的XML文檔。就像這樣:使用XSLT將XML中的dateand轉換爲UTC時區

 

2011-11-29T04:15:22-08:00 

我想的時間爲UTC,並在下面的表格使用XSLT輸出轉換日期:

 

2011-11-29 12:15:22 

如何能不能做到?

在此先感謝。

回答

0

XSLT-1.0沒有日期格式化功能。

因此,你必須與它的工作與文字 - 見相關的問題Format a date in XML via XSLT

其他可能的方法是創建擴展功能(見@ 0xA3執行答案同樣的問題在MSXSL和.Net的情況下)

7

以下XPath 2.0表達產生的期望字符串值

translate(
    string(
     adjust-dateTime-to-timezone(
      xs:dateTime('2011-11-29T04:15:22-08:00'), 
      xs:dayTimeDuration('PT0H') 
          ) 
     ), 
    'TZ', 
    ' ' 
      ) 

XSLT - 基於驗證

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:sequence select= 
    "translate(
     string(
      adjust-dateTime-to-timezone(
       xs:dateTime('2011-11-29T04:15:22-08:00'), 
       xs:dayTimeDuration('PT0H') 
           ) 
      ), 
     'TZ', 
     ' ' 
       ) 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉化是在任何XML文檔(未使用),XPath表達式求值和該評價的結果是施加輸出

2011-11-29 12:15:22