【问题标题】:How to format a date using XSLT?如何使用 XSLT 格式化日期?
【发布时间】:2012-09-10 06:56:50
【问题描述】:

尝试将日期转换为特定格式但出现错误:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.openapplications.org/oagis/9"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:variable name="origDate" select="row[@num='6']/cell[@num='2']"/>
<PromisedDeliveryDateTime><xsl:value-of select="format-date(xs:date($origDate), '[Y] [M] [D]')" /></PromisedDeliveryDateTime>

但我得到了错误:

转换 XQMessage 部分失败,索引=0(无效日期“30.11.2011”
(非数字部分)

【问题讨论】:

    标签: xml xslt date-format


    【解决方案1】:

    xs:date() 构造函数需要一个 ISO 日期格式的字符串:2011-11-30。要以其他格式处理日期,您首先需要将它们转换为 ISO 格式,例如

    string-join(reverse(tokenize($in, '.')), '-')
    

    【讨论】:

    • 谢谢!但还是有问题:&lt;xsl:variable name="origDate" select="row[@num='6']/cell[@num='2']"/&gt; &lt;xsl:variable name="origDate" select="string-join(reverse(tokenize($origDate, '.')), '-')"/&gt; &lt;PromisedDeliveryDateTime&gt;&lt;xsl:value-of select="format-date(xs:date($origDate), '[Y] [M] [D]')" /&gt;&lt;/PromisedDeliveryDateTime&gt; Failed to transform XQMessage part, index=0 (Invalid date "----------" (Year is less than 四位数)
    • @VextoR 没有人会阅读评论提要中的 XML。更新您的问题并提及您正在使用的 XSLT 处理器。
    猜你喜欢
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多