我需要使用XSL格式化日期。例如:
<xsl:choose>
<xsl:when test="$rawdate">
<xsl:variable name="dt" as="xs:date" select="$rawdate"/>
<xsl:value-of select="format-date($dt, '[D01]-[MNn,*-3]-[Y0001]')"/>
</xsl:when>
<xsl:otherwise>
N/A
</xsl:otherwise>
</xsl:choose>
如果$rawdate
的格式正确(例如YYYY-MM-DD),这将起作用。但是,如果格式不正确(例如DD-MM-YYYY),format-date()
函数将失败,处理将停止。$rawdate
来自一个手工编辑的XML文件,因此有人可能会无意中将日期格式化错误。
我想做的是捕捉format-date()
何时会失败(或已经失败),这样我就可以优雅地处理它,而不是停止转换。这可能吗?
使用castable as xs:date
<xsl:choose>
<xsl:when test="$rawdate castable as xs:date">
<xsl:variable name="dt" as="xs:date" select="$rawdate"/>
<xsl:value-of select="format-date($dt, '[D01]-[MNn,*-3]-[Y0001]')"/>
</xsl:when>
<xsl:otherwise>
N/A
</xsl:otherwise>
</xsl:choose>