javascript - XSLT Format Date to MM DD YYYY -


i trying format xml date using xslt/x-path.

i have: postdate="2014-03-27" i'd to render as: march 27, 2014.

i have read xslt may not way go. javascript better way? can please offer assistance?

thank you!

robin

here xslt , xpath solutions. if going process @ client-side (browser) have stick xslt 1.0 solution (or use javascript). if generate result somewhere else (standalone or server-side), might able use xslt2/xpath3 compatible processor.

xpath 3.0 solution

format-date(//*/@postdate, '[mnn] [d01], [y0001]') 

xslt solutions

source xml:

<message postdate="2014-03-27">some text</message> 

xslt 2.0 stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0">     <xsl:output method="html"/>     <xsl:template match="message">         <date>             <xsl:value-of select="format-date(@postdate, '[mnn] [d01], [y0001]')"></xsl:value-of>         </date>     </xsl:template> </xsl:stylesheet> 

xslt 1.0 stylesheet

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">     <xsl:output method="html"/>     <xsl:template name="month-name">         <xsl:param name="month"/>         <xsl:if test="$month = 1">january</xsl:if>         <xsl:if test="$month = 2">february</xsl:if>         <xsl:if test="$month = 3">march</xsl:if>         <xsl:if test="$month = 4">april</xsl:if>         <xsl:if test="$month = 5">may</xsl:if>         <xsl:if test="$month = 6">june</xsl:if>         <xsl:if test="$month = 7">july</xsl:if>         <xsl:if test="$month = 8">august</xsl:if>         <xsl:if test="$month = 9">september</xsl:if>         <xsl:if test="$month = 10">october</xsl:if>         <xsl:if test="$month = 11">november</xsl:if>         <xsl:if test="$month = 12">december</xsl:if>         </xsl:template>      <xsl:template name="format-iso-date">         <xsl:param name="iso-date"/>         <xsl:variable name="year" select="substring($iso-date, 1, 4)"/>         <xsl:variable name="month" select="substring($iso-date, 6, 2)"/>         <xsl:variable name="day" select="substring($iso-date, 9, 2)"/>         <xsl:variable name="month-name">             <xsl:call-template name="month-name">                 <xsl:with-param name="month" select="$month"/>             </xsl:call-template>         </xsl:variable>         <xsl:value-of select="concat($month-name, ' ',$day, ', ', $year)"/>     </xsl:template>      <xsl:template match="message">         <date>             <xsl:call-template name="format-iso-date">                 <xsl:with-param name="iso-date" select="@postdate"/>             </xsl:call-template>         </date>     </xsl:template> </xsl:stylesheet> 

xslt output:

<date>march 27, 2014</date> 

you can use date functions in exslt extension: http://www.exslt.org/


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -