2015-01-07 117 views
0

我有一个要求,我必须格式化传入请求中的日期值。我能够提取值,但它没有得到正确的格式。使用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> 

回答

2

看起来你使用substring(.,9,2)一个字符短的T03一部分。

应该substring(.,9,3)

+1

由于panhandel其工作:) –

+0

关于你的编辑,如果有手动添加吨的要求,你只是想更改子串(,9,2),以子(。,10,2) – panhandel