需要将文本值‘2012-03-19’转换为日期类型,然后提取年份组件。
<xsl:variable name="dateValue" select="2012-03-19"/>
<xsl:variable name="year" select="year-from-date(date($dateValue))"/>
我正在使用 Saxon 2.0,但它在提示
date
函数不存在;我查看了 Saxon 的文档并找不到该功能,所以这显然是问题所在,但我找不到合适的替代品。
最佳答案
我不认为 date()
应该是一个函数,你想要 xs:date()
数据类型。
添加 xs
命名空间,然后前缀 xs:date()
.
以下样式表,使用任何格式良好的 XML 输入,将产生 2012
:
<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="dateValue" select="'2012-03-19'"/>
<xsl:variable name="year" select="year-from-date(xs:date($dateValue))"/>
<xsl:value-of select="$year"/>
</xsl:template>
</xsl:stylesheet>
请注意,您还必须引用您的
select
在您的“日期值”xsl:variable
.
关于date - 如何将文本转换为日期,然后获取年份值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9744587/