Reputation: 167
My XML has a date string which looks likes this
<row id="1">
<DocDueDate>12/06/2016 12:00:00 AM</DocDueDate>
</row>
I am trying to format it using XSLT
<shipdate>
<!--YYYY-DD-MM-->
<xsl:variable name="var_shipdate" select="row/DocDueDate" />
<xsl:value-of select="concat(substring($var_shipdate, 7, 4), '-', substring($var_shipdate, 1, 2), '-', substring($var_shipdate, 4, 2) ) " />
</shipdate>
This produces an output like this
<shipdate>
20161206
</shipdate>
Buy my string concatenation fails in the xslt and produces a wrong output if my date format changes from MM/DD/YYYY
to MM/D/YYYY
or M/D/YYYY
or M/DD/YYYY
So how do I manipulate my string concatenation to produce an output like YYYYMMDD
irrespective of my source xml format..
I need to pad it with zeros but how do i split my source string on '/'
Upvotes: 1
Views: 80
Reputation: 116982
Try it this way:
<shipdate>
<!--YYYY-DD-MM-->
<xsl:variable name="date" select="substring-before(row/DocDueDate, ' ')" />
<xsl:variable name="year" select="substring-after(substring-after($date, '/'), '/')" />
<xsl:variable name="month" select="substring-before($date, '/')" />
<xsl:variable name="day" select="substring-before(substring-after($date, '/'), '/')" />
<xsl:value-of select="$year" />
<xsl:value-of select="format-number($month, '-00')" />
<xsl:value-of select="format-number($day, '-00')" />
</shipdate>
Upvotes: 1