【问题标题】:How to convert text to date, then get year value?如何将文本转换为日期,然后获取年份值?
【发布时间】:2012-04-02 10:16:42
【问题描述】:
需要将文本值“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
xslt
xslt-2.0
saxon
【解决方案1】:
我不认为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>
请注意,您还必须在“dateValue”xsl:variable 中引用您的 select。