In the below example we are trying to convert 'date' from one format to other like as want to convert into UTC format e.g. 2021-07-26T18:37:15.490Z
Can anyone help.
INPUT XML:
<?xml version="1.0" encoding="UTF-8"?>
<date>
<finish_dstamp>20190716140831</finish_dstamp>
</date>
EXISTING OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<date>
<finish_dstamp>20190716140831</finish_dstamp>
</date>
XSLT CODE:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Reference URL: https://xsltfiddle.liberty-development.net/pNEj9dB/1
You could also parse out the date and time components with regex. An example of regex and capture groups to construct the dateTime value (which you could also parse as dateTime to ensure is valid)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="finish_dstamp/text()">
<xsl:sequence select="replace(., '(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})', '$1-$2-$3T$4:$5:$6')"/>
</xsl:template>
</xsl:stylesheet>
An example using xsl:analyze-string
:
<xsl:template match="finish_dstamp/text()">
<xsl:analyze-string select="." regex="(\d{{4}})(\d{{2}})(\d{{2}})(\d{{2}})(\d{{2}})(\d{{2}})">
<xsl:matching-substring>
<xsl:sequence select="xs:dateTime(
string-join((
string-join((regex-group(1),regex-group(2),regex-group(3)), '-'),
string-join((regex-group(4),regex-group(5),regex-group(6)), ':')),
'T')) "></xsl:sequence>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:sequence select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
You could parse out the components of the date and time, and then use the fn:dateTime()
function with xs:date()
and xs:time()
params, and put that in a specialized template matching on the finish_dstamp/text()
node:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="finish_dstamp/text()">
<xsl:sequence select="dateTime(
xs:date(
concat(
substring(., 1, 4),
'-',
substring(., 5, 2),
'-',
substring(., 7, 2)
)
),
xs:time(
concat(
substring(., 9, 2),
':',
substring(., 11, 2),
':',
substring(., 13, 2)
)
)
)"/>
</xsl:template>
</xsl:stylesheet>
You could parse out the components of dateTime and use the xs:dateTime()
constructor:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="finish_dstamp/text()">
<xsl:sequence select="xs:dateTime(
concat(
substring(., 1, 4),
'-',
substring(., 5, 2),
'-',
substring(., 7, 2),
'T',
substring(., 9, 2),
':',
substring(., 11, 2),
':',
substring(., 13, 2)
)
)"/>
</xsl:template>
</xsl:stylesheet>