XSLT格式化日期为MM DD YYYY [英] XSLT Format Date to MM DD YYYY
问题描述
我正在尝试使用XSLT / X-Path格式化XML日期。
我有: PostDate =2014-03-27
我想呈现为: 2014年3月27日
。
我已经看到,XSLT可能不是要走的路。 JavaScript是一个更好的方法吗?有人可以提供一些帮助吗?
谢谢!
Robin
这里有一些XSLT和XPath解决方案。如果要在客户端(浏览器)进行处理,您将不得不遵守XSLT 1.0解决方案(或使用JavaScript)。如果您在其他地方(独立或服务器端)生成结果,您可能可以使用兼容XSLT2 / XPath3的处理器。
XPath 3.0解决方案
格式日期(// * / @ PostDate,'[MNn] [D01],[Y0001] )
XSLT解决方案
$ b $来源XML :
< Message PostDate =2014-03-27 >一些文本< / Message>
XSLT 2.0样式表:
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =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样式表
<?xml version =1.0encoding =UTF-8?>
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =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> 7月< / 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 =yearselect =substring($ iso-date,1,4)/>
< xsl:variable name =monthselect =substring($ iso-date,6,2)/>
< xsl:variable name =dayselect =substring($ iso-date,9,2)/>
< xsl:variable name =month-name>
< xsl:call-template name =month-name>
< xsl:with-param name =monthselect =$ 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-dateselect =@ PostDate/>
< / xsl:call-template>
< / date>
< / xsl:template>
< / xsl:stylesheet>
XSLT输出:
< date> 2014年3月27日< / date>
您还可以使用日期
EXSLT扩展程序: http://www.exslt.org/
I am trying to format an XML date using XSLT/X-Path.
I have: PostDate="2014-03-27"
I'd like to to render as: March 27, 2014
.
I have read that XSLT may not be the way to go. Is JavaScript a better way? Can someone please offer some assistance?
Thank you!
Robin
Here are some XSLT and XPath solutions. If you are going to process this at the client-side (browser) you will have to stick to the XSLT 1.0 solution (or use JavaScript). If you generate your result somewhere else (standalone or server-side), you might be able to use a 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 also use the date
functions in the EXSLT extension: http://www.exslt.org/
这篇关于XSLT格式化日期为MM DD YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!