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 .
source share