【问题标题】:XSLT Format Date to MM DD YYYYXSLT 格式日期为 MM DD YYYY
【发布时间】:2014-04-14 13:56:56
【问题描述】:

我正在尝试使用 XSLT/X-Path 格式化 XML 日期。

我有:PostDate="2014-03-27" 我想渲染为:March 27, 2014

我已经读到 XSLT 可能不是要走的路。 JavaScript 是更好的方法吗?有人可以提供一些帮助吗?

谢谢!

罗宾

【问题讨论】:

  • 使用 XPath 3.0(或使用 XSLT 2.0)你可以简单地使用:format-date(//date, '[MNn] [D01], [Y0001]') 来获得你想要的。但似乎您将在仅支持 XSLT 1.0 的浏览器中处理它。那样的话,使用 JS 可能会更简单

标签: javascript date xslt format


【解决方案1】:

这里有一些 XSLT 和 XPath 解决方案。如果您要在客户端(浏览器)处理此问题,则必须坚持使用 XSLT 1.0 解决方案(或使用 JavaScript)。如果您在其他地方(独立或服务器端)生成结果,则可以使用与 XSLT2/XPath3 兼容的处理器。

XPath 3.0 解决方案

format-date(//*/@PostDate, '[MNn] [D01], [Y0001]')

XSLT 解决方案

源 XML

<Message PostDate="2014-03-27">Some text</Message>

XSLT 2.0 样式表

<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 样式表

<?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 输出:

<date>March 27, 2014</date>

您还可以在 EXSLT 扩展中使用date 函数:http://www.exslt.org/

【讨论】:

  • 非常感谢!这太棒了!
【解决方案2】:

如果您在使用 Microsoft 的 MSXML XSL 1.0 处理器时遇到问题,这里还有另一种格式化日期的方法。您需要使用 Javascript/JScript(或其他语言,例如 C#)。

以这种方式添加代码时,不要忘记使用 CDATA 部分来确保 XSL 处理器跳过代码部分中的某些符号。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">

<msxsl:script language="jscript" implements-prefix="user"><![CDATA[
   function jsdate() 
   {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 
    var today = dd+'/'+mm+'/'+yyyy;

    return today; 
   }
//]]></msxsl:script>

   <xsl:template match= "/">
        <xsl:apply-templates select="MySection"/>
    </xsl:template>
    <xsl:template match="MySection">
        <IssueDate><xsl:value-of select="user:jsdate()"/></IssueDate>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多