Format a Date in Xml via Xslt

Format a date (string) with XSLT

The answer (full solution), taking some of what Christian Mosz & valdi_bo suggested above, and a final wonderfully helpful solution and explanation from @friedemann_bach (from a separate question I asked) is this, as the full XSL I needed:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ms="urn:schemas-microsoft-com:xslt">

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="now">
<now>
<xsl:variable name="srcDateTime" select="//Envelope/Body/send_message/user/now"/>
<xsl:variable name="srcDate" select="substring-before($srcDateTime, 'T')" />
<xsl:variable name="outDate" select="format-date(xs:date($srcDate),'[D01].[M01].[Y0001]')"/>
<xsl:value-of select="$outDate" disable-output-escaping="yes"/>
</now>
</xsl:template>
</xsl:stylesheet>

Whilst the first two answers here provided the solution to the date format, the issue I then had to contend with was not understanding why all the other tags were being ripped out. The first (additional) template fixes that (it serves to change nothing, so that the second template overrides "no change" only on the node I select in the second template.
Many thanks to everyone that helped. Being a noob (learning through simply HAVING to do it & get it done), I needed the full solution AND the explanation. I know it's a little "please do it for me", but it all adds to the knowledge, and that's kinda the point. Thanks all again.

Formatting a date in XSLT

You need to quote the values in your selects to make them strings:

<xsl:variable name="demoDate" select="'2017-06-21'" />

and

<xsl:param name="format" select="'yyyy-MM-dd'" />

Format a date in XML via XSLT

Here are a couple of 1.0 templates that you can use:-

<xsl:template name="formatDate">
<xsl:param name="dateTime" />
<xsl:variable name="date" select="substring-before($dateTime, 'T')" />
<xsl:variable name="year" select="substring-before($date, '-')" />
<xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
<xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
<xsl:value-of select="concat($day, ' ', $month, ' ', $year)" />
</xsl:template>

<xsl:template name="formatTime">
<xsl:param name="dateTime" />
<xsl:value-of select="substring-after($dateTime, 'T')" />
</xsl:template>

Call them with:-

    <xsl:call-template name="formatDate">
<xsl:with-param name="dateTime" select="xpath" />
</xsl:call-template>

and

    <xsl:call-template name="formatTime">
<xsl:with-param name="dateTime" select="xpath" />
</xsl:call-template>

where xpath is the path to an element or attribute that has the standard date time format.

How to format the date in xslt?

If you are using XSLT 1.0, you might find the date formatting features in EXSLT - Date and Times useful, in particular format-date. If you are using XSLT 2.0, you can use the built-in function described in Formatting Dates and Times.

As en example of the latter, if you want to output a date on the format "Jan 01, 2020", you'd write

<xsl:value-of select="format-date($d, '[MNn,*-3] [D01], [Y0001]')"/>

Format date in xslt

If you want to use XSLT 2.0 then you need to convert your custom date respectively dateTime format into an xs:dateTime and then you can use the format-dateTime function that XSLT 2.0 provides (see http://www.w3.org/TR/xslt20/#format-date):

<xsl:template match="LastSend">
<!-- 01/07/2013 16:38:18 -->
<xsl:variable name="dt" as="xs:dateTime" select="xs:dateTime(concat(substring(., 7, 4), '-', substring(., 4, 2), '-', substring(., 1, 2), 'T', substring(., 12)))"/>
<xsl:attribute name="subcaption" select="format-dateTime($dt, 'Last sent on [F] [D01] [MNn] [Y0001] at [H01]:[m01]')"/>
</xsl:template>

Take the above second argument "picture string" as an example on how to format a dateTime, you might need to adjust it for your needs, based on the picture string arguments documented in the XSLT 2.0 specification.

Formatting date in xslt

I think format-number will do the trick.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />

<xsl:variable name="date" select="'2007-3-19'" />
<xsl:template name="format_date" >
<xsl:param name ="date" />
<xsl:variable name ="year" select="substring-before($date, '-')" />
<xsl:variable name ="month_and_day" select="substring-after($date, '-')" />
<xsl:variable name ="day" select="substring-after($month_and_day, '-')" />
<xsl:variable name ="month" select="substring-before($month_and_day, '-')" />
<xsl:value-of select="$year"/>
<xsl:value-of select="format-number($month, '00')"/>
<xsl:value-of select="format-number($day, '00')"/>
</xsl:template>

<xsl:template match="/" >
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

Format Date and Time in XSLT

You need to work a bit harder at it:

<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 1, 4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 12, 8)"/>


Related Topics



Leave a reply



Submit