首页 > 解决方案 > XSLT 1.0 (xsltproc) - 将日期格式从 yyyy-mm-ddthh-mm-ssz 转换为 yyyy-MM-dd HH:mm:ss.SSS Z

问题描述

输入 XML:

<testng-results>
<suite>
<test>
    <class>
        <test-method name="ABC" started-at="2019-03-13T21:26:52Z"></test-method>
        <test-method name="XYZ" started-at="2019-03-13T21:27:15Z"></test-method>
    </class>
</test>
</suite>
</testng-results>

我当前的 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
  <Suite>
 <xsl:for-each select="testng-results/suite/test/class/test-method">
    <test>
       <xsl:attribute name="test_name">
          <xsl:value-of select="@name" />
       </xsl:attribute>
      <start_time>  </start_time>
    </test>
 </xsl:for-each>
  </Suite>

所需的输出.XML:

<Suite>
  <test test_name="ABC">
<start_time>2019-03-13 21:26:52.000 +0000 </start_time>
 </test>
<test test_name="XYZ">
<start_time>2019-03-13 21:26:52.000 +0000 </start_time>
 </test>
</Suite>

我必须从“started-at”值中获取日期并将其转换为 yyyy-MM-dd HH:mm:ss.SSS Z 格式以生成输出 xml。

我曾尝试使用 format-dateTime 函数,但 xsltproc (XSLT 1.0) 不支持它。

标签: datexsltxslt-1.0

解决方案


AFAICT,您要做的就是用T空格替换 ,并追加.000 +0000而不是Z

<start_time>
    <xsl:value-of select="translate(@started-at, 'TZ', ' ')"/>
    <xsl:text>.000 +0000</xsl:text>
</start_time>

推荐阅读