首页 > 解决方案 > XSLT 中的字符串到日期转换返回未找到函数

问题描述

我正在尝试确定当前日期是否介于 XSLT/XPath 中的其他两个日期之间。我发现 xs:date(STRING) 将日期字符串转换为日期值,然后我可以与 current-date() 进行比较。

我的问题是在尝试这个时,我收到一个错误,说该函数不存在。dateTime 显然也不存在。这是我正在使用的名称空间的代码。

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/04/xpath-functions/"
xmlns:xs="http://www.w3.org/2001/XMLSchema/">


        <xsl:if test = "fn:current-dateTime lt fn:date(EndDateActive)
                        and fn:current-dateTime gt fn:date(StartDateActive)">

结束日期和开始日期字符串的格式正确 afaik (yyyy-mm-dd),我也尝试了以下选项,它们都告诉我该函数不存在:

xs:date(EndDateActivate)
xsl:date(EndDateActivate)
fn:dateTime(EndDateActivate)
xs:dateTime(EndDateActivate)
xsl:dateTime(EndDateActivate)

标签: xmlxsltxpath

解决方案


有几个问题:

  • fn:current-dateTime()是一个函数。你错过了()
  • fn命名空间是,http://www.w3.org/2005/xpath-functions不是http://www.w3.org/2005/04/xpath-functions/
    • 在 XSLT 中,您不需要使用fn:名称空间前缀。你可以使用current-dateTime()
  • Schema 命名空间是http://www.w3.org/2001/XMLSchema, not http://www.w3.org/2001/XMLSchema/(删除尾随/)
  • 您正在尝试xs:dateTime比较xs:date
    • 而不是current-dateTime(),使用current-date()

对您的命名空间声明进行以下调整:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"

和你的@test

<xsl:if test="fn:current-date() lt xs:date(EndDateActive)
          and fn:current-date() gt xs:date(StartDateActive)">
</xsl:if>

推荐阅读