Convert 31-DEC-2016 to 2016-12-31

随声附和 提交于 2019-12-18 09:44:40

问题


I want to convert the 31-DEC-2016 i.e., dd-mmm-yyyy to yyyy-mm-dd in the XSLT using format-dateTime function but the output is not as expected.Can anyone help on this?

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

I want to get the value for this particular thing.31-DEC-2016 : This is the input and i have to transform over here in the code

Once that is converted, How to concat the the value T00:00:00 to the date??


回答1:


You cannot use the format-dateTime() function on a string that is not a valid dateTime (or the format-date() function on a string that is not a valid date). You need to process the string using string functions first.

Try:

<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>

Calling this template with a datestring parameter of "31-DEC-2016" will return a value of "2016-12-31".


Example of call (mostly guessing, not having seen the input):

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


In XSLT 2.0, you can define a function instead of a named template. The following stylesheet:

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>

will return:

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



回答2:


If you're formatting only dates, I could suggest

format-date(date, format)

Maybe this can help you,

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



来源:https://stackoverflow.com/questions/40868419/convert-31-dec-2016-to-2016-12-31

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!