Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a date in XML via XSLT

When I use the XML serializer to serialize a DateTime, it is written in the following format:

<Date>2007-11-14T12:01:00</Date> 

When passing this through an XSLT stylesheet to output HTML, how can I format this? In most cases I just need the date, and when I need the time I of course don't want the "funny T" in there.

like image 332
peterchen Avatar asked Feb 01 '09 14:02

peterchen


People also ask

How can I change the date format in XML?

Can use the following conversion code to convert UTC format string to any other DateTime format. string result = Convert. ToDateTime("2011-02-04T00:00:00+05:30"). ToString("MM/dd/yyyy h:mm:ss tt");

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What is regex in XSLT?

The regex-group() function names which matched string you want to use inside of the xsl:matching-substring element; pass it a 1 to get the first, a 2 to get the second, and so forth. The example above uses it to plug the three matched values inside new city, state, and zip elements created for the output.


1 Answers

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.

like image 62
AnthonyWJones Avatar answered Oct 31 '22 12:10

AnthonyWJones