【问题标题】:XSLT custom date formatting - GSAXSLT 自定义日期格式 - GSA
【发布时间】:2014-06-02 07:44:57
【问题描述】:

我正在尝试在动态导航中格式化 XSLT,以便以特定格式呈现日期。我的代码生成以下错误:

 "An unknown error occurred."

XSLT 代码是:

<xsl:template name="CustomDate-DN">
    <xsl:param name="d"/>
    <xsl:value-of select="format-date($d, '[D01] [MN,*-3] [Y0001]', 'en', (), ())"/>
</xsl:template>

<xsl:template match="PV" mode="display_value">
    <xsl:param name="js_escape"/>
    <xsl:choose>
        <!-- Customizations - Fancy Date -->
        <xsl:when test="../@T = 4">
            <xsl:call-template name="CustomDate-DN">
                <xsl:with-param name="d" select="@V"/>
            </xsl:call-template>
        </xsl:when>
        <!-- End of Customization -->
    ...

如果我替换

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

<xsl:value-of select="$d"></xsl:value-of>

它似乎有效,但日期格式错误。

我将不胜感激。谢谢。

更新:我的日期目前看起来像dd/mm/yyyy。我正在使用 xslt 2.0。我认为问题在于我将字符串传递给 format-date 函数。该函数需要一个日期。我不确定如何将dd/mm/yyyy 字符串转换为日期。

【问题讨论】:

  • 您确定您使用的是 XSLT 2.0 处理器吗? format-date 是 XSLT/XPath 2.0 中的新功能。如果您使用 XSLT 2.0 处理器,当您收到错误时,$d@V 的外观如何?
  • 目前看起来像 dd/mm/yyyy。我正在使用 xslt 2.0。我认为问题在于我将字符串传递给 format-date 函数。该函数需要一个日期。我不确定如何将 dd/mm/yyyy 字符串转换为日期?

标签: xslt xslt-2.0


【解决方案1】:

dd/mm/yyyy 格式表示日期的字符串不是有效的xs:date 并且不能在format-date() 中使用。

但您可以解析字符串并将其转换为ISO 8601 日期,该日期有效的xs:date类型。在 XSLT 2.0 中实现这一点的一种方法是使用 &lt;xsl:analyze-string&gt; 以正则表达式提取年、月和日部分。然后您可以重建 ISO 8601 格式的日期并将结果存储在一个新变量中,您可以将其传递给format-date()

<xsl:template name="CustomDate-DN">
    <xsl:param name="d"/>

    <xsl:variable name="iso-date">
        <xsl:analyze-string select="$d" regex="(\d{{1,2}})/(\d{{1,2}})/(\d{{4}})">
            <xsl:matching-substring>
                <xsl:value-of select="regex-group(3)"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="regex-group(2)"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="regex-group(1)"/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:variable>

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

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2021-08-06
    • 2011-03-01
    • 1970-01-01
    • 2017-11-10
    相关资源
    最近更新 更多