User501
User501

Reputation: 347

Need to format the date as per the input xml file

Need to change the date format from 123121(mmddyy) to 12/31/2021(mm/dd/yyyy)

Input XML I'm having:

<section>
<Plans>
<Date>123121</Date>
</Plans>
</section>

<p>date: <keyword keyref="Cost:Date:date4"/></p>

XSL I have tried:

<xsl:template match="keyword[contains(@keyref, 'Cost:Date:date4')]">
        <xsl:param name="section" as="element()" tunnel="yes">
            <empty/>
        </xsl:param>
        <keyword keyref="Cost:Date:date4">
            <xsl:call-template name="format_variable">
                <xsl:with-param name="cur_keyref" select="@keyref"/>
                <xsl:with-param name="cur_value"
                    select="$section//Plans/Date"/>
                <xsl:with-param name="cur_format" select="'date4'"/>
            </xsl:call-template>
        </keyword>
    </xsl:template>
    
          <xsl:template name="format_variable">
        <xsl:param name="cur_keyref" as="xs:string">MISSING</xsl:param>
        <xsl:param name="cur_value" as="xs:string">MISSING</xsl:param>
        <xsl:param name="cur_format" as="xs:string">MISSING</xsl:param>
        <xsl:choose>
        <xsl:when test="$cur_format = 'date4'">
                <xsl:value-of select="format-dateTime($cur_value, '[M01]/[D01]/[Y0001]')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of
                    select="concat('MISSING_FORMAT_', $cur_keyref, '_', $cur_value, '_[', $cur_format, ']')"
                />
            </xsl:otherwise>
            </xsl:choose>
            </xsl:template>

When I'm trying the above code its showing the below error:

Invalid dateTime value "123121" (Too short)

Please somebody help me out get the 12/31/2021(mm/dd/yyyy) value on the output.

Upvotes: 0

Views: 189

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

You are trying to use the format-dateTime() function on something that is not a valid dateTime.

What can't you do simply:

<xsl:value-of select="replace(Date, '(.{2})(.{2})(.{2})', '$1/$2/20$3')"/>

Note that this is assuming all your dates will be in the 21st century. Otherwise you would need additional logic to identify the century.

Upvotes: 1

Related Questions