【问题标题】:Convert 31-DEC-2016 to 2016-12-31将 2016 年 12 月 31 日转换为 2016 年 12 月 31 日
【发布时间】:2016-11-29 23:33:38
【问题描述】:

我想使用 format-dateTime 函数在 XSLT 中将 31-DEC-2016 即 dd-mmm-yyyy 转换为 yyyy-mm-dd,但输出不符合预期。有人可以帮忙吗?

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

我想获得这个特定事物的价值。31-DEC-2016:这是输入,我必须在代码中进行转换

转换后,如何将值 T00:00:00 连接到日期??

【问题讨论】:

    标签: xslt xslt-1.0 xslt-2.0 exslt


    【解决方案1】:

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

    试试:

    <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”调用此模板将返回值“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>
    

    【讨论】:

    • 如何在我的 xslt 中使用这个模板?我想将此模板应用于特定变量
    • @srinivaskalyan 请编辑您的问题并提供示例 - 请参阅minimal reproducible example
    • 这一切都很好,谢谢你,但有一点帮助。该服务仅接受日期时间格式。因此,一旦上述模板中的日期可用,如何将时间附加到它?
    • @srinivaskalyan 只需添加&lt;xsl:text&gt;T00:00:00&lt;/xsl:text&gt;
    • 我想将文本添加到我从模板中获得的同一日期
    【解决方案2】:

    如果您只格式化日期,我可以建议

    格式-日期(日期,格式)

    也许这对你有帮助,

    http://www.sixtree.com.au/articles/2013/formatting-dates-and-times-using-xslt-2.0-and-xpath/

    【讨论】:

    • 谢谢,但在 SOA 中,当我使用此功能时,我遇到了一些错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2020-09-26
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多