使用XSLT格式化日期时间

时间:2015-01-07 20:33:25

标签: datetime xslt

我有一个要求,我必须格式化传入请求中的日期值。我能够提取值,但它没有正确格式化。

以下是输入请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <Invoice Version="3.0">
         <Header>
            <ThisDocumentIdentifier>
               <DocumentIdentifier>0000001007128564</DocumentIdentifier>
            </ThisDocumentIdentifier>
            <ThisDocumentDateTime>
               <DateTime DateTimeQualifier="On">20140429T031659Z</DateTime>
            </ThisDocumentDateTime>
            </Header>
      </Invoice>
   </soapenv:Body>
</soapenv:Envelope>

日期值 20140429T031659Z

需要输出 2014-04-29T03:16:59Z

以下是代码:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"  xmlns:dp="http://www.datapower.com/extensions">
    <xsl:template match="*[local-name()='DateTime']">
        <xsl:variable name="FormatDatetime">
            <xsl:value-of select="concat(substring(., 1, 4), '-', substring(., 5, 2), '-', substring(., 7, 2),substring(.,9,2),':',substring(.,11,2),':',substring(.,13,2),'Z')"/>
        </xsl:variable>
        <xsl:message dp:priority="debug"> Formatted date= <xsl:value-of select="$FormatDatetime"/>
        </xsl:message>
    </xsl:template>
</xsl:stylesheet>

这是我得到的输出 的 2014-04-29T0:31:65Z

由于某些原因,其中一个号码被删除了,我不确定为什么?

任何人都可以告诉我在哪里做错了吗?

我也添加了'T'。

<xsl:value-of select="concat(substring(., 1, 4), '-', substring(., 5, 2), '-', substring(., 7, 2),'T',substring(.,9,2),':',substring(.,11,2),':',substring(.,13,2),'Z')"/>

输出为 的 2014-04-29TT0:31:65Z

这是更新的工作代码,感谢panhandel。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"  xmlns:dp="http://www.datapower.com/extensions">
    <xsl:template match="*[local-name()='DateTime']">
        <xsl:variable name="FormatDatetime">
            <xsl:value-of select="concat(substring(., 1, 4), '-', substring(., 5, 2), '-', substring(., 7, 2),substring(.,9,3),':',substring(.,11,2),':',substring(.,13,2),'Z')"/>
        </xsl:variable>
        <xsl:message dp:priority="debug"> Formatted date= <xsl:value-of select="$FormatDatetime"/>
        </xsl:message>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

看起来您使用substring(.,9,2)将T03部分缩短了一个字符。

应为substring(.,9,3)