'Convert a node datetime to Epouch time using XSLT

I am getting a xml from other system having a CREATION_DATE node.

<CREATION_DATE>20/05/2021 10:10:00 </CREATION_DATE>

I have to convert this node to unix mili-second time something like

<CREATION_DATE>161273897651200 </CREATION_DATE>

My XSLT is not able to convert it.

<xsl:if test="/CREATION_DATE != ''" >
    <creation_date>
    
<xsl:variable name="dateTimeValue"/>
<xsl:value-of select="(/CREATION_DATE,'0')[.
        ne ''][1]"/>

<xsl:sequence select=
"( xs:dateTime($dateTimeValue) - xs:dateTime('1970-01-01T00:00:00') )
div
xs:dayTimeDuration('PT1S')
"/>

</creation_date>
</xsl:if>

Not sure what is wrong, any help please



Solution 1:[1]

The CREATION_DATE value isn't a valid dateTime formatted string. So you need to first construct a properly formatted dateTime value to use for $dateTimeValue of 2021-05-20T10:10:00:

<xsl:if test="/CREATION_DATE != ''" >
  <creation_date>
    <xsl:variable name="dateTimeSeq" select="tokenize(/CREATION_DATE, ' ')"/>
    <xsl:variable name="dateTimeValue" select="concat(string-join(reverse(tokenize($dateTimeSeq[1], '/')), '-'), 'T', $dateTimeSeq[2])"/>         
    <xsl:sequence select=
                "( xs:dateTime($dateTimeValue) - xs:dateTime('1970-01-01T00:00:00') )
                div
                xs:dayTimeDuration('PT1S')
    "/>     
</creation_date>
    </xsl:if>
               

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mads Hansen