2016-11-29 40 views
0

我想使用format-dateTime函数在XSLT中将2016年31-DEC-2016即dd-mmm-yyyy转换为yyyy-mm-dd,但输出结果为不像预期的那样。任何人都可以帮忙吗?Convert 31-DEC-2016 to 2016-12-31

<ns1:QuoteDate> 
      <xsl:value-of select='concat(xp20:format-dateTime(/Quote/QuoteHeader/QuoteDate,"[Y0001]-[M01]-[D01]"),"T00:00:00")'/> 
     </ns1:QuoteDate> 

我想要得到的价值为这个特殊的thing.31-DEC-2016:这是输入和我曾经被转化为这里改造过的代码

,如何Concat的数值T00:00:00到日期?

回答

1

对于不是有效日期时间的字符串(或对不是有效日期的字符串的format-date()函数),您不能使用format-dateTime()函数。您需要首先使用字符串函数处理字符串。

尝试:

<xsl:template name="convertDate"> 
    <xsl:param name="datestring"/> 

    <xsl:variable name="d" select="substring-before($datestring, '-')"/> 
    <xsl:variable name="MMM" select="substring-before(substring-after($datestring, '-'), '-')"/> 
    <xsl:variable name="m" select="string-length(substring-before('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC', $MMM)) div 3 + 1"/> 
    <xsl:variable name="y" select="substring-after(substring-after($datestring, '-'), '-')"/> 

    <xsl:value-of select="$y"/> 
    <xsl:value-of select="format-number($m, '-00')"/> 
    <xsl:value-of select="format-number($d, '-00')"/> 
</xsl:template> 

调用此模板, “31-DEC-2016” 的datestring参数将返回 “2016年12月31日” 的值。呼叫的


实施例(主要是猜测,未见过的输入):

<ns1:QuoteDate> 
    <xsl:call-template name="convertDate"> 
     <xsl:with-param name="datestring" select="/Quote/QuoteHeader/QuoteDate"/> 
    </xsl:call-template> 
</ns1:QuoteDate> 


在XSLT 2.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" 
xmlns:my="http://www.example.com/my" 
exclude-result-prefixes="xs my"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:function name="my:convertDate"> 
    <xsl:param name="string"/> 
    <xsl:variable name="parts" select="tokenize($string, '-')"/> 
    <xsl:variable name="m" select="index-of (('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'), $parts[2])"/> 
    <xsl:sequence select="xs:date(concat($parts[3], format-number($m, '-00'), format-number(number($parts[1]), '-00')))" /> 
</xsl:function> 

<xsl:template match="/"> 
    <result> 
     <xsl:value-of select="my:convertDate('9-MAR-2016')"/> 
    </result> 
</xsl:template> 

</xsl:stylesheet> 

将返回:

<?xml version="1.0" encoding="utf-8"?> 
<result>2016-03-09</result> 
+0

如何使用这个模板在我的XSLT?我想为特定变量应用此模板 –

+0

@srinivaskalyan请编辑您的问题并提供示例 - 请参阅[mcve]。 –

+0

谢谢。它会在任何月份工作吗? –