XSLT format Date - delete milliseconds

I am using XSLT to create an XML file. A DateTime has milliseconds. I need to have XML output without milliseconds.

The format must be YYYY-MM-DDTHH:MM:SS

For instance:

XML shows date as: 2012-12-341T09:26:53.132-0500

But it should be: 2012-12-341T09:26:53

+4
source share
3 answers

If you are using XSLT2, see this function: http://www.w3.org/TR/xslt20/#function-format-dateTime . This line of drawing should provide you with what you want:

 format-dateTime($dateTime,'[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]') 
+1
source

If all values ​​are dateTime and have . , you can use substring-before() :

 substring-before('2012-12-341T09:26:53.132-0500', '.') 

You can use substring() to select the first 20 characters:

 substring('2012-12-341T09:26:53.132-0500', 0, 21) 
+1
source

This XPath expression produces the desired result regardless of whether the line contains a period or hyphen, period or hyphen or not, and does not rely on the number of digits used for the year, month, day

  substring-before(concat(substring-before(concat(substring-after(.,'T'), '.'), '.'), '-'), '-') 

Here is a simple XSLT transformation using this XPath expression :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="dt/text()"> <xsl:value-of select="substring-before(., 'T')"/> <xsl:text>T</xsl:text> <xsl:value-of select= "substring-before(concat(substring-before(concat(substring-after(.,'T'), '.'), '.'), '-'), '-') "/> </xsl:template> </xsl:stylesheet> 

When this conversion is applied to this test XML document:

 <t> <dt>2012-12-341T09:26:53.132-0500</dt> <dt>2012-12-355T09:34:56</dt> <dt>2012-12-355T09:34:56-0500</dt> <dt>2012-12-13T9:34:5-0500</dt> <dt>2012-12-344T09:12:34.378-0500</dt> </t> 

the desired, correct result is output:

 <t> <dt>2012-12-341T09:26:53</dt> <dt>2012-12-355T09:34:56</dt> <dt>2012-12-355T09:34:56</dt> <dt>2012-12-13T9:34:5</dt> <dt>2012-12-344T09:12:34</dt> </t> 

Explanation

Proper use of sentries .

0
source

All Articles